设为首页 收藏本站
查看: 565|回复: 0

[经验分享] Nginx: 99: Cannot assign requested address for upstream-12213582

[复制链接]

尚未签到

发表于 2018-11-10 08:31:34 | 显示全部楼层 |阅读模式
  A few days ago, I ran into the following interesting Nginx error message.
[crit] 12889#0: *32401195 connect() to 127.0.0.1:80 failed (99: Cannot assign requested address) while connecting to upstream  My configuration was very simple. This was an Nginx proxy that did all the SSL encryption and sent traffic to a Varnish instance, running on port :80 on localhost. The big takeway here is that it was a pretty high traffic Nginx proxy.
  Even with keepalive enabled in the nginx upstream, the error popped up. But what did it mean?
TCP ports and limits
  It's good to know a thing or two about networking besides just servers once in a while. The problem occurred because the server couldn't get a free TCP port quickly enough to make the connection to 127.0.0.1.
$ ss -s  
Total: 3130 (kernel 3431)
  
TCP:   51582 (estab 2866, closed 48611, orphaned 92, synrecv 0, timewait 48611/0), ports 35279
  The ss tool gives you stats on the sockets/ports on the server. In this case, I had 51.582 TCP sessions in use (either active, closed, awaiting to be closed, ...).
  A normal server has around 28.000 possible TCP ports it can use to make a TCP connection to a remote (or local) system. Everything that talks via an IP address will pick a free port from this range to serve as source port for the outgoing connection. This port range is defined by the ip_local_port_range sysctl parameter.
$ cat /proc/sys/net/ipv4/ip_local_port_range  
3276861000
  

  
$ sysctl net.ipv4.ip_local_port_range
  
net.ipv4.ip_local_port_range = 3276861000
  The format is "minimum maximum" port. So 61000 – 32768 = 28 232 available source ports.
  An nginx SSL proxy that connects to a Varnish instance running on localhost will look like this in your netstat.
$ netstat -anp | grep 127.0.0.1  
...
  
tcp        0      0 127.0.0.1:37713         127.0.0.1:80            TIME_WAIT   -
  The key takeaways here the source connection 127.0.0.1:37713 that connects to its endpoint 127.0.0.1:80. For every source connection a new TCP source port is selected from the range in the ip_local_port_range parameter.
  The combination of a source IP, source port, destination IP and destination IP needs to be unique. This is what's called a quadruplet in networking terms. You likely can't (easily) change the source IP. The source port is dynamically picked. That only leaves the destination IP and the destination port that are free to play with.
Solving the source port limitation
  There are a couple of easy fixes. First, the ip_local_port_range can be increased on a Linux machine (for more reading material, see increase ip_local_port_range TCP port range in Linux).
$ echo 15000 64000 > /proc/sys/net/ipv4/ip_local_port_range  This effectively increases the total port range from its default 28 232 ports to 49 000 ports.
  If that's not enough, you can add more destination IPs to connect to. Remember that each connection consists of the 4 parts (called quadruplets) with source IP and source port, destination IP and destination port. If you can't change the source port or IP, just change the destination IPs.
  Consider this kind of upstream definition in Nginx;
upstream varnish {  
  server 127.0.0.1:80;
  
}
  Such a definition can be used in your nginx configurations with the proxy_pass directive.
server {  
  listen 443;
  
  ...
  
  location / {
  
    proxy_pass http://varnish;
  
    ...
  
  }
  
}
  Now if you know that each server usually has 2 IPs or more, it's very easy to add more quadruplets to your networking stack by adding an addition IP to your Nginx upstream. You've already added 127.0.0.1, but your server will have another IP (its public or DHCP IP) that you can safely add too, if your webserver binds to all ports.
upstream varnish {  
  server 127.0.0.1:80;
  
  server 31.193.180.217:80;
  
  server 10.50.5.1:80;
  
  ...
  
}
  Every IP you add in your upstream is effectively adding 49.000 local ports to your networking stack. You can even add non-routable local IPs to your server, as interface aliases, just to use as new destination IPs for your proxy configurations.
  Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting in Belgium, a general web geek, public speaker and podcaster. If you're interested in keeping up with me, have a look at my podcast and weekly newsletter below. For more updates, follow me on Twitter as @mattiasgeniar.
  zhuanzi:  https://ma.ttias.be/nginx-cannot-assign-requested-address-for-upstream/



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-633065-1-1.html 上篇帖子: nginx限速方法以及报错处理 下篇帖子: 解决nginx不支持websocket
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表