V
V
VladVol2017-04-23 18:16:48
Nginx
VladVol, 2017-04-23 18:16:48

Why doesn't socket.io work through a proxy?

There is an application in /web/myapp/. The client is connected to:

let socket = io.connect('http://domain.com/web/myapp/ ', {path: '/myapp'});

The proxy is configured like this:
upstream myapp_socket {
   ip_hash;
   server 127.0.0.1:8080;
}

location /web/myapp/ {
   proxy_pass http://myapp_socket/;

   proxy_read_timeout 1000s;

   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
   proxy_set_header Host $http_host;
   proxy_set_header X-Forwarded-For $remote_addr;
   proxy_redirect off;
   proxy_pass_request_headers on;
   proxy_cache off;
   proxy_buffering off;
}

Question: why doesn't all this work without this pointing to socket.io???
location ~ ^/(socket\.io) {
   proxy_pass http://myapp_socket;
   
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade"; 
}

Does the socket work for you without this location?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2017-04-23
@Sanasol

because socket.io is "listening to path" /socket.io and not just "directly"
https://github.com/socketio/socket.io/blob/master/...

V
VladVol, 2017-04-23
@VladVol

Then in order to run the second application, will I have to duplicate this rule under the new upstream?
How is it done correctly?

upstream myapp_2_socket {
   ip_hash;
   server 127.0.0.1:2255;
}

location /web/myapp2/ {
   proxy_pass http://myapp_2_socket/;
   ...
}

location ~ ^/(socket\.io) {
   proxy_pass http://myapp_2_socket;
   ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question