websocket

最近项目部署到正式环境的时候发现页面websocket连接错误。如下:

img 其中与本地环境的区别就是正式环境配置了nginx。而我通过真实路径访问正式环境项目,也能正常连接。经查阅相关资料,在nginx.conf 配置文件中location 加入如下参数即可:

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";



    注意需要加入

其中第一行是告诉nginx使用HTTP/1.1通信协议,这是websoket必须要使用的协议。

第二行和第三行告诉nginx,当它想要使用WebSocket时,响应http升级请求。

socket.io 官方issues有关于这个问题的讨论,链接:https://github.com/socketio/socket.io/issues/1942

vim /data/pdfae-qianbao/nginx/conf/vhost/ws2.conf

upstream tomcat_service {
   server 172.31.5.3:8086;
}

server {
    listen 80;
    server_name  uatws2.pdfae.ph;

    location / {
        proxy_pass http://tomcat_service;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $Server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;   #注意此行需要存在,否则会报400错误

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

}
Copyright © 高程程 all right reserved,powered by Gitbook修订于: 2021-05-18 21:14:35

results matching ""

    No results matching ""