Ümit Aparı
Ümit Aparı

Reputation: 568

Proxy Passing Socket.IO connections on nginx not working

I am trying to proxy pass a node.js-socket.io app with nginx.

The client is an html file with some javascript in it;

<html>
<head>
<script src="socket.io.js"></script>
<script>
    var socket = io('http://localhost:80');
    socket.on('welcome', function(data){
        console.log('Server says:' + data);
        socket.emit('client-response', 'thank you!');
    });
</script>
</head>
<body>
Socket.io
</body>
</html>

And the server block that supposed to proxy pass in nginx.conf file is this;

server {
        listen       80;
        listen       [::]:80;
        #root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_pass "http://localhost:2156";
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

I have my node.js app up and running in port "2156".

When I test this, the client tries to reach the socket.io on the port 80 and fails with a 404 error (Because nginx was supposed the pass the proxy to the port 2156 but it didn't).

What am I missing here?

Upvotes: 2

Views: 9464

Answers (1)

&#220;mit Aparı
&#220;mit Aparı

Reputation: 568

Edit: I've changed the client to connect at "http://localhost/socket.io/" and rewrote the nginx.conf like this:

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        root         /usr/share/nginx/html;

        location /socket.io/ {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
            proxy_pass http://localhost:2156/socket.io/;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

And it worked.

Upvotes: 6

Related Questions