I am running a websocket server on my Digital Ocean droplet using pm2. Also, I am using websocket/ws websocket library.
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.

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.

