Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large number of Port forwarding in nginx

I'm trying to add mapping for 20K Ports (range [40k-60k]) in the nginx configuration. This config is added to nginx.conf

stream{
    server {
        listen 40000;
        listen 40001;
        .
        .
        .
        listen 60000;
        proxy_pass <backend-url>:$server_port;
     }
}

Everything works jolly-good when number of mappings is < 500. But when it's increased to 20K mappings, the delay in response is tremendous. Any work-around or any other method to add port-forwarding?

like image 905
Abhishek Avatar asked Mar 08 '18 10:03

Abhishek


People also ask

What is the maximum number of connections available in NGINX?

With NGINX, every open connection equates to at least one or sometimes two open files. By setting the maximum number of connections to 4096 , we are essentially defining that every worker can open up to 4096 files.

How many requests NGINX can handle?

Once the rate limit of 10 requests per second is exceeded by a single client accessing /api/ , NGINX returns a “429 Too many requests” error to the client.

Does NGINX listen multiple ports?

To make Nginx Listen on multiple ports for a single virtual host file, you can add multiple listen directives. If you want to make Nginx listen for different virtual hosts on different ports, you can use different ports in listen directive in different virtual host files. It's that easy!

How many requests can NGINX load balancer handle?

The configuration allows bursts of up to 12 requests, the first 8 of which are processed without delay. A delay is added after 8 excessive requests to enforce the 5 r/s limit. After 12 excessive requests, any further requests are rejected.


2 Answers

Since Nginx 1.15.10 you can specify a range of ports on listen directive.

Port ranges (1.15.10) are specified with the first and last port separated by a hyphen:

listen 127.0.0.1:12345-12399;
listen 12345-12399;

More info: http://nginx.org/en/docs/stream/ngx_stream_core_module.html#listen

like image 112
Fernando Mariano Avatar answered Nov 14 '22 23:11

Fernando Mariano


I'd try to do accomplish it via iptables instead of nginx

https://www.cyberciti.biz/faq/linux-port-redirection-with-iptables/

You can easily redirect incoming traffic by inserting rules into PREROUTING chain of the nat table. You can set destination port using the REDIRECT target

i.e.

iptables -t nat -A PREROUTING -p tcp --dport 1:65535 -j REDIRECT --to-ports 10000

and listen port 10000 in nginx

Related discussion: https://superuser.com/questions/440324/iptables-how-to-forward-all-external-ports-to-one-local-port

like image 26
ffeast Avatar answered Nov 14 '22 22:11

ffeast