S
S
source20032016-10-15 02:14:20
Nginx
source2003, 2016-10-15 02:14:20

Setting up nginx ports for socket.io?

I do not know if I formulated the question correctly, but now I will try to explain the problem.
The server is running nginx, which redirects domains to different ports.
It was conceived like this: all domains will go to port 80, and the domains on which Node.js applications should run will go to ports 8080, 8000, 3000, 1337, etc., in order to run several node applications, and a site in PHP.
It looked like this:

server {

  listen 80 default_server;
  server_name localhost;
}

server {

  listen 80;
  server_name example.com;

  location / {
    proxy_pass http://IP_ADRESS:1337;
  }

}

All sites go to port 80, and example.com goes to port 1337, where the Node.js application is launched.
Here's the problem:
When I needed to use Socket.IO, there was an error on example.com that redirected to IP_ADRESS:1337:
WebSocket connection to 'ws://example.com/socket.io/?EIO=3&transport=websocket&sid = y1fxSfbvA -eqW6ayAAAZ' failed: Error during WebSocket handshake: Unexpected response code: 400
at example.com, IP_ADRESS:80 opens, there is no such error and everything works fine.
How can I make it so that I can run node applications on different ports and work with socket.io?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Ernest Faizullin, 2016-10-15
@erniesto77

try something like this

upstream first_server {
    server 127.0.0.1:3000;
}

upstream second_server {
    server 127.0.0.1:1337;
}

  server {
    listen 80;
    server_name domain1.com;
    location / {
        proxy_pass http://first_server;
  }
}

  server {
    listen 80;
    server_name domain2.com;
    location / {
        proxy_pass http://second_server;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question