Connect Socket.io with nginx - bad request
dustgalaxy
HOBBYOP

2 months ago

Problem - client can't connect to socket.io service through nginx. Also i use socket.io namespaces like /plst_upds.

nginx conf:
resolver 127.0.0.11 valid=5s;

server {

listen 80;

server_name $DOMAIN;

client_header_timeout 120s;

client_body_timeout 120s;

send_timeout 120s;

root /usr/share/nginx/html;

index index.html;

location / {

try_files $uri $uri/ /index.html;

}

location /socket.io/ {

proxy_pass http://socket-service.railway.internal:8000/socket.io

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

proxy_read_timeout 86400s;

proxy_send_timeout 86400s;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Proto $scheme;

}

}

Server side:

sio = socketio.AsyncServer(

    async_mode="asgi",

    cors_allowed_origins=[

        "*",

    ],

)

app = socketio.ASGIApp(sio)
uvicorn.run(

    "main:app",

    host=settings.SELF_HOST, # "::"

    port=settings.SELF_PORT, # "8000"

    log_level=settings.SELF_LOG_LEVEL.lower(),

    reload=settings.SELF_RELOAD,

    )

Client side:

export const plst_upds_socket = io(

  'https://proj.up.railway.app' + '/plst_upds',

  {

    withCredentials: true,

  },

Logs nginx:

100.64.0.2 - - [13/Oct/2025:11:17:38 +0000] "POST /socket.io/?EIO=4&transport=polling&t=1h38ihkt&sid=cejHGsjkNSe44YlsAADq HTTP/1.1" 400 75 "https://proj.up.railway.app/dashboard" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0" "91.195.184.192"

100.64.0.2 - - [13/Oct/2025:11:17:38 +0000] "POST /socket.io/?EIO=4&transport=polling&t=1h3afcth&sid=cejHGsjkNSe44YlsAADq HTTP/1.1" 400 75 "https://proj.up.railway.app/dashboard" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0" "91.195.184.192"

100.64.0.3 - - [13/Oct/2025:11:17:38 +0000] "GET /socket.io/?EIO=4&transport=websocket&sid=cejHGsjkNSe44YlsAADq HTTP/1.1" 403 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0" "91.195.184.192"

Logs server:

INFO: fd12:225a:8c4b:0:1000:70:c928:74c4:51088 - "GET /socket.io?EIO=4&transport=polling&t=1h36irkr HTTP/1.1" 200 OK

INFO: fd12:225a:8c4b:0:1000:70:c928:74c4:51092 - "POST /socket.io?EIO=4&transport=polling&t=1h38ihkt&sid=cejHGsjkNSe44YlsAADq HTTP/1.1" 400 Bad Request

INFO: fd12:225a:8c4b:0:1000:70:c928:74c4:51112 - "POST /socket.io?EIO=4&transport=polling&t=1h3afcth&sid=cejHGsjkNSe44YlsAADq HTTP/1.1" 400 Bad Request

INFO: fd12:225a:8c4b:0:1000:70:c928:74c4:51122 - "WebSocket /socket.io?EIO=4&transport=websocket&sid=cejHGsjkNSe44YlsAADq" 403

I don't know how it fixes. I need help...

$10 Bounty

2 Replies

Railway
BOT

2 months ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


Hi!

It looks like your server is returning error code 400. This is an issue with your code, not Railway.

Also, you should not use Nginx on Railway because of Railway's dynamic internal IP assignment. It will break when you restart the service. I would recommend setting up a Caddyfile instead: https://caddyserver.com/docs/caddyfile


Loading...