V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
aries910
V2EX  ›  NGINX

用 nginx tcp 转发,并发高的时候报错怎么解决

  •  
  •   aries910 · 2022-02-23 08:49:09 +08:00 · 2710 次点击
    这是一个创建于 764 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个 1 核 1g 的 ECS 连接数到七八百的时候,就开始大量报错 connect() to [fe80::60e9:7eae:2ab5:7f4b%14]:1234 failed (101: Network is unreachable) while connecting to upstream, client: 149.86.33.169, server: 0.0.0.0:5487, upstream: "[fe80::60e9:7eae:2ab5:7f4b%14]:1234", bytes from/to client:0/0, bytes from/to upstream:0/0 看网上说禁用 IPV6 可以,但尝试后无效...

    如下这些设置都有,还请大佬给点建议缺少什么配置 worker_connections 8192; worker_rlimit_nofile 65535 tcp_nodelay on;

    14 条回复    2022-02-23 16:44:06 +08:00
    jifengg
        1
    jifengg  
       2022-02-23 08:59:15 +08:00
    是 1m 的带宽么,是不是网络占满了。
    Davic1
        2
    Davic1  
       2022-02-23 09:05:54 +08:00
    1. 把 keep alive 打开, 重用 tcp 连接. 比如用 http1.1 协议
    2. 在 upstream 上设置 keepalive
    3. 把 linux 的临时端口设置大一些 (local_range_port)
    4. 把 linux 的文件句柄数设置大一些.
    vanton
        3
    vanton  
       2022-02-23 09:09:53 +08:00
    贴一下你的 nginx 配置。
    高并发出问题看着就是连接数满了。
    aries910
        4
    aries910  
    OP
       2022-02-23 10:22:56 +08:00
    @vanton worker_processes 1;

    events {
    worker_connections 8192;
    }

    worker_rlimit_nofile 65535;

    stream {
    open_log_file_cache off;

    upstream aaa {
    server aaa.com:1234 weight=5 max_fails=1 fail_timeout=30s;
    }
    server {
    listen 1234;
    proxy_connect_timeout 30s;
    proxy_timeout 30s;
    proxy_pass aaa;
    tcp_nodelay on;
    resolver 8.8.8.8 ipv6=off;
    }
    }
    aries910
        5
    aries910  
    OP
       2022-02-23 10:23:13 +08:00
    @jifengg 是 1m 的
    aries910
        6
    aries910  
    OP
       2022-02-23 10:35:31 +08:00
    @Davic1
    1.请问下 tcp 转发,可以用 http1.1 吗,具体怎么设置呢
    2.搜了下,大部分都是在 stream 下设置 keepalive=16
    3.实际也确实用的是比较大的端口
    4.好像 worker_rlimit_nofile 65535 这句已经把 linxu 的文件句柄设置到 65535 了
    dongpengfei1
        7
    dongpengfei1  
       2022-02-23 10:39:57 +08:00
    你应该限制 nginx 的并发数和到上游的流量增加缓存数和等待时间,然后从系统上关闭 ipv6 。
    aries910
        8
    aries910  
    OP
       2022-02-23 11:02:01 +08:00
    @dongpengfei1
    关键我希望 nginx 能承载尽可能多的连接,并发越高越好
    就当是个压力测试吧
    以此为目的,设置 nginx 以达到尽可能高的并发
    Davic1
        9
    Davic1  
       2022-02-23 11:02:28 +08:00
    @aries910 4 层转发就和 http 没关系了.

    你做压测的时候看 watch -n 1 "netstat -natpl|wc -l" 最高能到多少. ngx 做反向代理角色相当于客户端,local_port_range 是要设置一下的.

    内核的参数我贴个你参考下
    fs.file-max = 1048576
    fs.nr_open = 1048576
    kernel.msgmax = 65536
    kernel.shmall = 4294967296
    kernel.shmmax = 68719476736
    kernel.sysrq = 0
    net.core.netdev_max_backlog = 2000000
    net.core.rmem_default = 699040
    net.core.rmem_max = 50331648
    net.core.somaxconn = 65535
    net.core.wmem_default = 131072
    net.core.wmem_max = 33554432
    net.ipv4.ip_local_port_range = 1025 65000
    net.ipv4.ip_nonlocal_bind = 1
    net.ipv4.tcp_fin_timeout = 10
    net.ipv4.tcp_keepalive_time = 300
    net.ipv4.tcp_max_orphans = 3276800
    net.ipv4.tcp_max_syn_backlog = 655360
    net.ipv4.tcp_max_tw_buckets = 6000000
    net.ipv4.tcp_mem = 94500000 915000000 927000000
    net.ipv4.tcp_rmem = 32768 699040 50331648
    net.ipv4.tcp_wmem = 32768 131072 33554432
    net.ipv4.tcp_slow_start_after_idle = 0
    net.ipv4.tcp_synack_retries = 2
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_syn_retries = 2
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_window_scaling = 1
    net.ipv4.tcp_fastopen = 3
    net.ipv4.tcp_mtu_probing = 1
    vm.swappiness = 0
    Lax
        10
    Lax  
       2022-02-23 11:47:52 +08:00
    cat /proc/<pid>/limits 确认一下实际生效的 limits 配置。

    那个 aaa.com 是内网服务还是公网服务?网络延时有多大?

    测试的话,要把这个配置 max_fails=1 调高,尤其是网络不稳定的时候。
    dongpengfei1
        11
    dongpengfei1  
       2022-02-23 11:47:57 +08:00
    @aries910 主要你的上游受不了呀,都不接收消息了。那你增加缓存数,和等待时间试试。
    cxh116
        12
    cxh116  
       2022-02-23 13:13:18 +08:00 via Android
    你的 upstream aaa 只能处理那么多请求,增大 nginx 没啥用,优化你的后端服务的并发吧。
    NikoXu
        13
    NikoXu  
       2022-02-23 15:23:51 +08:00
    tcp 转发用防火墙不就行了
    NAPATA
        14
    NAPATA  
       2022-02-23 16:44:06 +08:00 via iPhone
    nginx 设置 http1.1 的时候 keepalive 配多少比较好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2795 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 15:27 · PVG 23:27 · LAX 08:27 · JFK 11:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.