【问题标题】:Large number of Port forwarding in nginxnginx中大量端口转发
【发布时间】:2018-08-16 15:46:47
【问题描述】:

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

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

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

【问题讨论】:

标签: nginx port iptables portforwarding


【解决方案1】:

从 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;

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

【讨论】:

  • 注意你还应该设置解析器和server_port resolver 127.0.0.1; proxy_pass you_server_ip:$server_port;
【解决方案2】:

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

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

您可以通过将规则插入到 nat 表的 PREROUTING 链。您可以使用设置目标端口 重定向目标

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

nginx中监听10000端口

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

【讨论】:

    猜你喜欢
    • 2023-01-18
    • 2014-02-14
    • 2019-05-23
    • 2017-07-18
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    相关资源
    最近更新 更多