Proxying WebSocket connections and ephemeral port

2019-06-20 03:56发布

I'm designing an application that make will use of WebSocket to notify visitors quickly when changes occur. I'm planning to proxy the connections with Nginx, so they can share the same address as the regular HTTP portion. One thing I'm concerned about is the exhaustion of ephemeral ports. In the past, I have noticed problems when the number of connections between Nginx and the Node backend exceeds 25000. My question is, would the following config increases the limit to 100K?

upstream backends {
    server 127.0.0.1:5000;
    server 127.0.0.2:5000;
    server 127.0.0.3:5000;
    server 127.0.0.4:5000;
}

The limit on open files will need to be raised accordingly, of course.

Assuming the technique works, can it be used with external servers? How hard is it to assign a range of IP addresses to an interface? Networking is not my forte...

1条回答
smile是对你的礼貌
2楼-- · 2019-06-20 04:55

Please try proxy_bind . It

Makes outgoing connections to a proxied server originate from the specified local IP address. Parameter value can contain variables (1.3.12).

So if our nginx proxy server can have several IP addresses your problem can be resolved. e.g.

location / {
   ...
   proxy_bind $picked_ip;
   proxy_pass http://backends;
}

The variable $picked_ip can be computed by a rewrite handler which runs before proxy_pass does.

查看更多
登录 后发表回答