在 Apache 中,我們有一些很好的解決方案來限制每個虛擬主機的頻寬使用,例如 mod_bandwidth。然而,自從我開始使用 nginx 以來,我找不到一種方法來限制和監控每個伺服器區塊的頻寬使用情況。
我想聽聽一些關於監控和限制 nginx 上每個伺服器區塊的頻寬使用的建議。
答案1
您可以使用limit_rate
限制頻寬。對我來說,這也適用於 nginx 的社群版本。
例如,整個伺服器區塊的頻寬可以限制為每秒 1000 位元組,如下所示:
server {
listen 80;
location / {
limit_rate 1k;
}
}
答案2
Nginx Plus 有內建選項來限制每個主機的頻寬。
如果你想透過 nginx 的社群版本來實現它,你有 2 個選擇:(因為它只包括每個 IP 的頻寬池。)
1:使用外部工具或ip-tables限制整個nginx進程的頻寬。
2:透過 proxy-pass 向 lighttpd 提供此特定內容,並在 lighttpd 中設定伺服器節流閥
server.kbytes-per-second = 6250
如果 downloads.domain.net 是我們想要限制的東西,Lighthttpd 將具有以下配置:
server.port = 81
server.document-root = "/path/to/downloads"
index-file.names = ( )
nginx:
server {
listen 80;
server_name downloads.domain.net;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:81;
}
}
當然你也可以透過特定的文件、資料夾等來代理。
最終:如果您不想超過特定的頻寬點,則使用此功能以及 ip-table 限制整個連接埠 80 流量(和 443 )是最終的解決方案。 (例如 VoIP 服務可能需要對 http 流量進行硬限制)