• 请不要在回答技术问题时复制粘贴 AI 生成的内容
voidmnwzp
V2EX  ›  程序员

nginx 反代 websocket,为什么被代理端写入错误 write: broken pipe ?

  •  
  •   voidmnwzp ·
    NullpointerW · Sep 10, 2024 · 2699 views
    This topic created in 611 days ago, the information mentioned may be changed or developed.

    客户端能与 nginx 握手成功,但接收不到数据,难道是 nginx 这边关闭了被代理端连接? 搞了快一天,Google 也搜不到,使劲浑身解数还是没辙,

    upstream target {
            server xxx.xxx.xxx.xxx:8080;
    }
     location /wss {
            proxy_pass http://target/ws;
            proxy_http_version 1.1;
            proxy_set_header Upgrade websocket;
            proxy_set_header Connection "Upgrade";
            proxy_redirect off;
            proxy_cache_bypass $http_upgrade;
        	proxy_set_header Host $host;
        	proxy_set_header X-Real-IP $remote_addr;
        	proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        	proxy_buffering off;
        	proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header Access-Control-Allow-Origin *;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header Upgrade $http_upgrade;
    	    proxy_set_header Connection $connection_upgrade;
    	    gzip off;
    	    proxy_set_header X-Forwarded-Ssl on;
    	    proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Frame-Options SAMEORIGIN;
            proxy_read_timeout 5d;
            proxy_send_timeout 5d;
            proxy_connect_timeout 5d;
            send_timeout 5ds;
            fastcgi_connect_timeout 5d;  
            fastcgi_read_timeout 5d;   
            fastcgi_send_timeout 5d;   
        }
    

    然后 golang 这边服务端写入时的错误为

    ws written err: write tcp xxx:8080->xxx:53376: write: broken pipe
    
    5 replies    2024-09-11 15:03:28 +08:00
    log4j
        1
    log4j  
       Sep 10, 2024
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    我这边只加了这 3 个就可以了。其余的没加
    nextvay
        2
    nextvay  
       Sep 10, 2024
    golang 这边的问题吧,链接断了
    montaro2017
        3
    montaro2017  
       Sep 11, 2024
    之前我是这样配的
    ```
    location /ws/ {
    proxy_pass http://127.0.0.1:8100/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    }
    ```
    voidmnwzp
        4
    voidmnwzp  
    OP
       Sep 11, 2024
    @montaro2017 试过了 没用 如果关闭 buffer 会提示 broken pipe ,不关闭的话会在上游的连接断开后 nginx 才会和客户端建立连接
    montaro2017
        5
    montaro2017  
       Sep 11, 2024
    @voidmnwzp #4 那我感觉就不像是 nginx 配置问题了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1217 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 17:45 · PVG 01:45 · LAX 10:45 · JFK 13:45
    ♥ Do have faith in what you're doing.