A
A
agentDonskoy2019-09-04 11:19:52
Nginx
agentDonskoy, 2019-09-04 11:19:52

How to run socket.io over WS[WSS] proxied to another server?

There is a server mysite.com Launched nginx version == 1.16.0. The link mysite.com/app is proxied to mysite-srv.com:30001. Mysite-srv.com is running nodejs and socket.io app on port == 30001. Client connects to mysite.com/app - everything works. nginx config:

nginx config
server {

  ...

  location /app/ {

    proxy_pass http://mysite-srv.com/app:30001;
  
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
  
  }
  
  location /socket.io/ {
    
    proxy_pass http://mysite-srv.com/app:30001;
    
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
  
  }

  ...

}

Now there is another client. I want to make it connect to mysite.com/app2 (which is proxied to mysite-srv.com:30002). And then I swam) When choosing the exchange protocol, socket.io sends a GET request to mysite.com/socket.io/(.*), to direct it to where I need, I corrected the nginx config:
nginx config (fixed)
server {

  ...

  location /app/ {

    location /app/1/ {

      proxy_pass http://mysite-srv.com/app:30001;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;

    }

    location /app/2/ {

      proxy_pass http://mysite-srv.com/app:30002;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;

    }

  }

  location /socket.io/ {

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host;

  #Здесь я направляю куда мне надо
    if ($http_referer ~* /app/1/.*) {
      proxy_pass http://mysite-srv.com/app:30001;
    }
    if ($http_referer ~* /app/2/.*) {
      proxy_pass http://mysite-srv.com/app:30002;
    }

  }
  ...

}

With this approach, WS does not work (only Polling), because http_referer is not. How to make socket.io work via WS[WSS] ?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question