如何为套接字传递 nginx 代理 url

How to pass nginx proxy url for socket

我在我的应用程序中使用套接字,我想从 nginx 作为代理传递套接字连接 url url.I 我就是这样做的

我的套接字代码

var socket = io.connect('/explorer/socket',{
      'reconnect': true,
      'reconnection delay': 500
    });

我的 nginx 配置文件

location /explorer/socket {
                proxy_pass    http://xxx.xxx.xx.xxx:3000;
        }

但它不工作,它正在连接到我的本地主机,但我想连接我在 nginx 中定义的代理 url。那么我如何在 io.connect 中传递代理 url 呢?

尝试如下更改您的 nginx.conf 的位置;

location /explorer/socket {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_pass http://xxx.xxx.xx.xxx:3000;
}

来源:http://nginx.com/blog/nginx-nodejs-websockets-socketio/

你必须像那样更改 nginx 配置

location /socket.io/ {
                proxy_pass    http://xxx.xxx.xx.xxx:3000;
                 proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “upgrade”;
proxy_read_timeout 86400;

access_log off;
error_log /opt/nginx/logs/websockets.error.log;


        }

1) /socket.io/ - 这将告诉 nginx 将您的套接字调用传递给给定的 ip

现在更改您的套接字代码

var socket = io.connect('/',{
      'reconnect': true,
      'reconnection delay': 500
    });