Not able to connect to socket.io through nginx

2019-06-05 19:08发布

问题:

I have nginx configured such that any php request goes to Apache server and any url with /node/ in it goes to the node server running on 8888 including a socket listener.

the nginx.conf is as below

server {
    listen       80;
    server_name   http://domain;

       location / {
                proxy_pass http://localhost:8080;

                }

      location /node {
            rewrite ^/node(.+)$ $1 break;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_pass http://localhost:8888;

            }

        }

Now the problem is that a socket.io request i.e io.connect('domain/node/') fails with the reposnse at client as "The requested URL /socket.io/1/ was not found on this server", response being served from apache!!. But any other node request goes to the node server as expected. only the socket.io request fails.

Also if I run the socket connect URL directly as seen in the browser request which looks something like
domain/node/socket.io/1/?t=1380201512328 works perfectly fine and I can see handshake authorized 8wGgJYUvNdwAdcqenxQd on node server.

回答1:

Try this on the clientside :)

var socket = io.connect('https://localhost', { path: '/node/socket.io' });

I'm having problems with this aswell, but you need to specify path so the client wont send requests to something like this:

GET https://localhost/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_sdk_p…sails_io_sdk_language=javascript&EIO=3&transport=polling&t=1432066804506-0 504 (Gateway Time-out)

Especially since you want it to look like (notice /node):

GET https://localhost/node/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_sdk_p…sails_io_sdk_language=javascript&EIO=3&transport=polling&t=1432066804506-0 504 (Gateway Time-out)