nginx 反代 websocket 断线


PS:官方文档说 Nginx 在 1.3 以后的版本才支持 websocket 反向代理,所以要想使用支持 websocket 的功能,必须升级到 1.3 以后的版本;

现出现一个问题每隔一段时间websocket断线1-2分钟,重启服务或者2分钟后会自动恢复,
服务层架构是nginx反代tomcat;

nginx的编译安装略过......

websocket配置如下:


 http {
     ......

     #websocket 需要加下这个
     map $http_upgrade $connection_upgrade {
         default upgrade;
         '' close;
    }

     ......
}

server {
        ......


        location  ^~  /websocket {
        proxy_pass http://www.test.com;

        proxy_redirect    off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        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";
        }

        ......
}

websocket nginx

旺财真好吃 11 years, 5 months ago

已经找到问题了,结贴;

nginx.conf
proxy_read_timeout 86400;

include http://www.test.com
keepalive 64;

Local answered 11 years, 5 months ago

Your Answer