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?
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.
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.
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!
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With