socket.io: Question: What could be the possible reasons for socket-io server returning 400 Bad Request via nginx?

Hi,

We are facing a scenario where a good percentage of requests going to /Socket.io/?... is returning with 400.

Default nginx access log results are littered with:

10.0.0.82 - - [04/Dec/2015:11:43:27 +0000] "GET /socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=websocket HTTP/1.1" 400 45 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"

The requests are not even reaching our overlying SailsJS server. We suspect something going wrong at socket-io layer. Any thought?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 29 (3 by maintainers)

Most upvoted comments

After a LOT of painstaking log-watching, this is our temporary solution for nginx config (to stop freaking out our servers.) The following directive checks for missing “Upgrade” header in request and returns with a 202 without hitting the node server.

location /socket.io {
    set $local_upgrade_failure "${arg___sails_io_sdk_platform}${arg_transport}${connection_upgrade}";
    if ( $local_upgrade_failure = "browserwebsocket" ) { return 202; break; }

 ... remain proxy configurations
}

Now, this does NOT fix the clients that are sending these requests, but surely frees up the server. Now, the question is @rauchg, why are some socket.io clients not sending the “upgrade” header? One pattern is that they are all Windows and a very few Linux machines. Did not spot a single OSX in one full hour of heavy usage.

I will then have to deep dive into socket.io - it’s being returned by socket.io with no additional message -

{ code: 3, message: “badRequest”}

I was having the very same issue, I faced this error when i was proxy my NodJS api from http://baseurl/api. Apparently I was missing ; (semicolons) at the end of nginx configs.

I was doing

proxy_http_version 1.1
proxy_set_header Upgrade $http_upgrade
proxy_set_header Connection "upgrade"

What worked

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

Yep missing semicolons; Please double check ur configs

The bug has been reported (and ignored) upstream: socketio/engine.io#283

There is no workaround. Clients will drop to xhr for that session. If using nginx and you run into “no live upstreams”, this is the cause, so a workaround is to set max_fails to 0, so nginx never considers the upstream “down”. Hacky workaround (esp. if your upstream ever does go down), but there it is.

Interesting discovery: The upgrade header is blank for the requests having 400 response. Forcing upgrade header to websocket stops the 400, but causes intermittent 499 error on nginx.