2013-01-03 59 views
5

Nginx上/ 1.0.12作爲運行在Debian 6.0.1的代理開始短時間運行後拋出以下錯誤:nginx的代理:connect()以IP:80失敗(99:無法分配請求的地址)

 
connect() to upstreamip:80 failed (99: Cannot assign requested address) 
while connecting to upstream, client: xxx.xxx.xxx.xxx, server: localhost, 
request: "GET/HTTP/1.1", upstream: "http://upstreamip:80/", 
host: "requesteddomain.com" 

並非所有的請求產生這個錯誤,所以我懷疑它與服務器的負載和某種限制它打的事情。

我曾嘗試將ulimit -n升至50k,並將worker_rlimit_nofile升至50k,但這似乎沒有幫助。 lsof -n顯示nginx共有1200行。 傳出連接是否有系統限制,可能會阻止nginx打開更多連接到其上游服務器?

回答

11

好像我剛剛找到了解決我自己的問題:通過

echo "10240 65535" > /proc/sys/net/ipv4/ip_local_port_range 

分配更多的輸出端口解決了這個問題。

0

修改的/etc/sysctl.conf:

net.ipv4.tcp_timestamps=1 
net.ipv4.tcp_tw_recycle=0 
net.ipv4.tcp_tw_reuse=1 
net.ipv4.tcp_max_tw_buckets=10000  #after done this: local ports decrease from 26000 to 6000(netstat -tuwanp | awk '{print $4}' | sort | uniq -c | wc -l) 

運行:

sysctl -p 
+0

我不確定這是否會有所幫助,因爲問題不是DOS攻擊導致大量TIME_WAIT,而只是大量的本來應該經過的正常流量,而不是因爲更快的TIME_WAIT超時而被殺死。 – mariow

+0

@mariow,在我的服務器上,有大量的傳出請求(抓取程序),所以TIME_WAIT快速重用是非常重要的。 – diyism

+1

net.ipv4.tcp_tw_recycle的破裂和Linux的4.12移除: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4396e46187ca5070219b81773c4e65088dac50cc 重複使用也很危險: https://vincent.bernat.im/en/blog/2014-tcp-time-wait-state-linux 最好不要使用這些選項。 – pva

1

每個TCP連接都必須有一個獨特的四SOURCE_IP:source_port:DEST_IP:dest_port

SOURCE_IP是很難改變,source_port是從ip_local_port_range中選擇的,但不能超過16位。剩下要調整的另一件事是dest_ip和/或dest_port。因此,添加一些IP別名上游服務器:

upstream foo { server ip1:80; server ip2:80; server ip3:80; }

其中IP1,IP2和IP3是在同一臺服務器不同的IP地址。

或者讓您的上游監聽更多端口可能更容易。

相關問題