nginx代理websocket,出现websocket: close 1006 (abnormal closure)错误

 

背景:

部署到服务器上的websocket程序,未加心跳,发现隔一阵子就会断开,或十分钟、或半小时的概率,出现1006 (abnormal closure)错误。

[Serve] conn closed with err. err:websocket: close 1006 (abnormal closure): unexpected EOF"

 

代理配置如下:

复制代码
location / {
        proxy_set_header Host $host;
        proxy_pass       http://test_view;
        proxy_redirect   off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /websocket/v2 {
        proxy_redirect off;
      proxy_read_timeout 60s; proxy_set_header X
-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto "http"; proxy_set_header Host $host; proxy_set_header X-NG-PRX-Host $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://test_view; }
复制代码

原因:

代理参数的 proxy_read_timeout 默认是60s,只要超过这段时间没有通信,就会自动断开tcp连接。

 

解决办法:

1、修改参数 proxy_read_timeout,比如在代理设置添加 proxy_read_timeout 300s;
2、添加心跳,心跳的间隔要小于默认的读超时proxy_read_timeout 60s,这样程序就会可以在60s的时间窗口内读到数据,不会被nginx断开连接。

 

\\

 

posted @   i潘小潘  阅读(12770)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示