Nginx - Error during WebSocket handshake: Unexpected response code: 502

Viewed 7244

I am running a websocket server on my Digital Ocean droplet using pm2. Also, I am using websocket/ws websocket library.

enter image description here

It is listening on port 3030. enter image description here

Using the command, netstat -l -p, I am able to see that it is indeed listening.

Below is the setup on my nginx sites-enabled configuration file. I am running static files in the / url, and running /socket url. And, /path/to/blah is modified for security reasons.

 31 server {
 30   . . . listen 80;
 29   . . . listen [::]:80;
 28 
 27   . . . server_name xxxx.com www.xxxx.com;
 26   . . . return 302 https://$server_name$request_uri;
 25 }
 24 
 23 server {
 22   . . . # SSL configuration
 21 
 20   . . . listen 443 ssl http2 default_server;
 19   . . . listen [::]:443 ssl http2 default_server;
 18   . . . server_name xxxx.com www.xxxx.com;
 17 
 16   . . . ssl_certificate /path/to/cert;
 15   . . . ssl_certificate_key /path/to/key;
 14 
 13   . . . location / {
 12   . . . . . .proxy_pass http://127.0.0.1:5000;
 11   . . . }
 10 
  9   . . . location /socket {
  8   . . . . . . proxy_set_header   X-Forwarded-For $remote_addr;
  7   . . . . . . proxy_set_header   Host $http_host;
  6   . . . . . . proxy_pass http://127.0.0.1:3030;
  5   . . . . . . proxy_http_version 1.1;
  4   . . . . . . proxy_set_header Upgrade $http_upgrade;
  3   . . . . . . proxy_set_header Connection 'upgrade';
  2   . . . . }
  1 }

When connecting to the websocket using wss://www.xxxx.com/socket. I get this error:

Error during WebSocket handshake: Unexpected response code: 502

Looking at the log file for Nginx located at /var/log/nginx. This is the error I am seeing.

[error] 31009#31009: *19 upstream prematurely closed connection while reading response header from upstream, client: X.X.XX.XXX, server: xxxxx.com, request: "GET /socket HTTP/1.1", upstream: "http://127.0.0.1:3030/socket", host: "www.xxxxx.com"

When inspecting the header for this websocket. This is what I got. enter image description here

The static asset is able to render fine, but not the websocket server. This tells me that at least wss://www.xxxx.com is able to contact my Nginx server at /socket, but it seems that the response is not right. When I looked into my pm2 log and error file, I see that it has outputted nothing. When I tested it with localhost, it worked fine. But, when deploying to server, it breaks.

2 Answers

I have found the solution to my problem.

I discovered is that if a problem like this arises it is usually issues related to the upstream, which in my case is the application running on port 3030. It turns out that my websocket was not doing what I expected it to do.

If others run into the issue as well. I recommend taking a really simple websocket that you know for sure works, and see if it works on Nginx.

I got this error when I forgot to start my server. So make sure the server is running :)

Related