nginx大量端口转发

Abh*_*hek 6 port iptables nginx portforwarding

我正在尝试在 nginx 配置中为 20K 端口(范围 [40k-60k])添加映射。这个配置被添加到 nginx.conf

stream{
    server {
        listen 40000;
        listen 40001;
        .
        .
        .
        listen 60000;
        proxy_pass <backend-url>:$server_port;
     }
}
Run Code Online (Sandbox Code Playgroud)

当映射数小于 500 时,一切都很好。但是当它增加到 20K 映射时,响应延迟是巨大的。任何解决方法或任何其他方法来添加端口转发?

小智 5

从 Nginx 1.15.10 开始,您可以在 listen 指令上指定一系列端口。

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;
Run Code Online (Sandbox Code Playgroud)

更多信息:http : //nginx.org/en/docs/stream/ngx_stream_core_module.html#listen


ffe*_*ast 3

iptables我会尝试通过而不是完成它nginx

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

您可以通过将规则插入 nat 表的 PREROUTING 链来轻松重定向传入流量。您可以使用 REDIRECT 目标设置目标端口

IE

iptables -t nat -A PREROUTING -p tcp --dport 1:65535 -j REDIRECT --to-ports 10000
Run Code Online (Sandbox Code Playgroud)

并监听端口10000nginx

相关讨论:https://superuser.com/questions/440324/iptables-how-to-forward-all-external-ports-to-one-local-port