nginx Cannot assign requested address TIME_WAIT解決

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 查看端口狀態linux

「Cannot assign requested address.」是因爲linux分配的客戶端鏈接端口用盡,沒法創建socket鏈接所致,雖然socket正常關閉,可是端口不是當即釋放,而是處於TIME_WAIT狀態,默認等待60s後才釋放。cookie

解決方法1 調低time_wait狀態端口等待時間:socket

  1. 調低端口釋放後的等待時間,默認爲60s,修改成15~30s sysctl -w net.ipv4.tcp_fin_timeout=30
  2. 修改tcp/ip協議配置, 經過配置/proc/sys/net/ipv4/tcp_tw_resue, 默認爲0,修改成1,釋放TIME_WAIT端口給新鏈接使用 sysctl -w net.ipv4.tcp_timestamps=1
  3. 修改tcp/ip協議配置,快速回收socket資源,默認爲0,修改成1 sysctl -w net.ipv4.tcp_tw_recycle=1

可能解決辦法2--增長可用端口: CCH:~ # sysctl -a |grep port_range net.ipv4.ip_local_port_range = 50000 65000 -----意味着50000~65000端口可用tcp

修改參數: $ vi /etc/sysctl.conf net.ipv4.ip_local_port_range = 10000 65000 -----意味着10000~65000端口可用ip

永久解決方案(重啓有效)資源

$ vi /etc/sysctl.confit

加入cookies

net.ipv4.ip_local_port_range = 1024 65535awk

net.ipv4.tcp_timestamps=1配置

net.ipv4.tcp_tw_recycle=1

net.ipv4.tcp_tw_reuse=1

改完後,執行命令「sysctl -p」使參數生效,不須要reboot。

另一種

vi /etc/sysctl.conf

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse=1 #讓TIME_WAIT狀態能夠重用,這樣即便TIME_WAIT佔滿了全部端口,也不會拒絕新的請求形成障礙 默認是0

net.ipv4.tcp_tw_recycle=1 #讓TIME_WAIT儘快回收 默認0

net.ipv4.tcp_fin_timeout=30

/sbin/sysctl -p 讓修改生效

##linux文件打開數限制

sysctl -w fs.file-max=10485760

msysctl -w net.ipv4.tcp_rmem=1024

sysctl -w net.ipv4.tcp_wmem=1024

sysctl -w net.ipv4.ip_local_port_range='1024 65535' #

sysctl -w net.ipv4.tcp_tw_recycle=1

sysctl -w net.ipv4.tcp_tw_reuse=1

echo '* soft nofile 1048576' >> /etc/security/limits.conf

echo '* hard nofile 1048576' >> /etc/security/limits.conf

相關文章
相關標籤/搜索