0

I am configuring Nginx to redirect incoming web socket requests to two different servers, but every time I get 404.

My Nginx configuration in loadbalancer.conf file in /etc/nginx/conf.d/ folder look like this:

upstream websocket {
    server localhost:5050; //web socket server1 running here
    server localhost:8080; //web socket server2 running here
}
server {
    listen      4141;
    server_name localhost:4141;

    location / {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Host $host;
        proxy_set_header Connection "upgrade";
    }
}

When I am hitting web socket servers directly (URL: ws://localhost:8080/app/socket/user1) then the request are working fine but when I am going through Nginx (URL: ws://localhost:4141/app/socket/user1) then the requests are not delivering to any of the web socket servers.

Update:

  1. I have tried to change the proxy_pass setting to directly the server1 (localhost:5050) then also the request is not going through.
  2. Nginx access log says the following error:

::1 - - [09/Jan/2023:10:57:07 +0530] "GET /app/socket/user1 HTTP/1.1" 404 197 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36"

Can someone please answer

  1. What is wrong with the configuration?
  2. Does Nginx provide more detailed logs, where can I check them?
balram rajput
  • 55
  • 2
  • 8
  • This is so strange, the issue was with the port, i just changed the nginx port to 8081 from 4141 and it worked. don't know why 4141 did not work. – balram rajput Jan 09 '23 at 06:41

1 Answers1

0

You can use ip_hash configuration for nginx up_stream blo

upstream websocket {
ip_hash;
server localhost:5050; //web socket server1 running here
server localhost:8080; //web socket server2 running here
}
server {
listen      4141;
server_name localhost:4141;

location / {
    proxy_pass http://websocket;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Host $host;
    proxy_set_header Connection "upgrade";
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 31 '23 at 00:52