大网数据广告活动
服务器运维问答

Scaling nginx beyond 45k connections

· 52CE 我爱测

I have a situation that means my nginx server has many long running open connections, however after a certain amount of connections it starts throwing an error 'Cannot assign requested address for upstream'. I've already gone ahe…

问题原文

原帖链接 · Ash · CC BY-SA 4.0

I have a situation that means my nginx server has many long running open connections, however after a certain amount of connections it starts throwing an error 'Cannot assign requested address for upstream'.

I've already gone ahead and increased the underlying system limits to the maximum (increasing open file limits, usable port ranges etc), and in terms of CPU/RAM the system is performing absolutely fine, it seems like the bottleneck is just the number of open connections/ports.

Is there really no way to have a single nginx server handle say 100k, or 200k concurrent open connections? I know more machines can be added to the equation, but then that disrupts things such as nginx rate limiting.

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

The error message you quoted indicates that the problem is not with the number of incoming connections, but the number of outgoing connections, from nginx to your backend web application.

You will have done something like this somewhere in your configuration:

proxy_pass http://127.0.0.1:8000;

Remember that a TCP connection is uniquely identified by source address and port and destination address and port. In this case it appears you have run out of source port numbers to make unique connections.

The obvious immediate fix is to switch to UNIX domain sockets. Your application must also listen on a UNIX domain socket. You might also need to arrange to have nginx's number of open files limit raised (e.g. raise LimitNOFILE= in its systemd unit) as well as that of your web app. The number of connections you can make this way is essentially unlimited.

proxy_pass unix:/run/myapp/myapp.sock;

A less obvious fix, if you can't make your app use UNIX domain sockets, is to have your web app listen on more than one address/port pair, and to have nginx rotate between them. This will double the number of connections you can make. If your web app already listens for both IPv4 and IPv6 connections, then a very quick and cheap fix is to have nginx alternate between them. You could also modify your app to listen on multiple ports.

upstream myapp {
   server 127.0.0.1:8000;
   server [::1]:8000;
}

or

upstream myapp {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
}

Then

proxy_pass http://myapp;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-02-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。