Nginx - 配置Wss协议

3,690 阅读1分钟

使用websocket的同学可能会发现域名在没配置https之前websocket是可以正常用的,但是配置了以后就连接失败了,下面是解决方案中的其中一种,希望可以帮到大家

  • 修改nginx.conf
   -    在需要连接的server下添加
        location /wss {
            proxy_pass http://127.0.0.1:端口;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header X-Real-IP $remote_addr;
        }
    #   如果是用Workerman或者GatewayWorker的同学端口可以写成框架内默认的8282
  • 重启Nginx
  • html重写
    ws = new WebSocket("wss://域名/wss");