WebSocket连接到'ws:// <ip> ..失败:WebSocket握手期间出错:意外响应代码:502

时间:2017-08-20 04:34:18

标签: nginx websocket socket.io nginx-reverse-proxy

我正在使用node,socket.io,nginx作为基于http的简单应用程序的套接字连接。下面是我的nginx.conf,我配置了websockets,我的socket io存在于socket / socket.io中。奇怪的是,当我在localhost和nginx中使用端口80运行节点应用程序时,相同的配置工作正常。不确定为什么它在linux中失败了哪个真实的IP地址。

http {
  upstream io_nodes {
    ip_hash;
    server IP:PORT; --> My application's IP and port
    server IP:PORT;

  }

  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 1000;

  # HTTP Server
  server {
    listen 80;
    server_name IP;
    server_tokens off;
    #proxy_ssl_verify off;

    # prevents 502 bad gateway error
    large_client_header_buffers 8 32k;

    location /dummy/ {
      root html;
      index dummy/index.html;
    }

    location /socket/socket.io {
    proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

      # prevents 502 bad gateway error
        proxy_buffers 8 32k;
        proxy_buffer_size 64k;

      proxy_pass http://io_nodes/;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
    }

    #location /socket.io/ {
    #  proxy_pass http://io_nodes/socket.io/;
    #}

    location /assistlog/socket.io {
    proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

    # prevents 502 bad gateway error
        proxy_buffers 8 32k;
        proxy_buffer_size 64k;

      proxy_pass http://10.73.52.187:4444/;
      proxy_redirect off;

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

      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;

    }
}
}

这是chrome中的控制台错误

WebSocket connection to 'ws://IP/socket/socket.io/?pathName=%2FDEMOUI%2Findex.html&EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 502

请提出解决方案。

1 个答案:

答案 0 :(得分:0)

检查清单: 首先使用https://websocket.org/echo.html

检查您的浏览器是否支持网络套接字

然后,如果您在组织代理下,则需要为本地地址传递代理。

让我们说,您的组织代理IP是x.x.x.x。您需要传递一些地址(从需要wss / ws连接的应用程序中)来不使用组织代理。

在Mac,Chrome中为示例:设置,搜索代理,然后您就可以看到“绕过这些主机和域的代理设置”。在此处添加您的应用程序IP