CodexBloom - Programming Q&A Platform

CentOS 8 - implementing NGINX Reverse Proxy Setup for WebSocket Connections

👀 Views: 96 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-09
nginx websocket centos reverse-proxy

I'm collaborating on a project where I'm having trouble with my NGINX reverse proxy configuration on CentOS 8 for handling WebSocket connections. My setup is supposed to forward requests from `https://mydomain.com/ws` to a WebSocket server running on `http://localhost:3000`. However, I'm working with the following behavior in the browser's console when trying to connect to the WebSocket: `WebSocket connection to 'wss://mydomain.com/ws' failed: behavior during WebSocket handshake: Unexpected response code: 400`. My NGINX configuration looks like this: ```nginx server { listen 443 ssl; server_name mydomain.com; ssl_certificate /etc/ssl/certs/mydomain.com.crt; ssl_certificate_key /etc/ssl/private/mydomain.com.key; location /ws { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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; } } ``` I've verified that the WebSocket server is running and accessible via `http://localhost:3000`. I can connect directly to it without any issues. I've also checked the firewall settings and they seem to be correctly allowing traffic on port 443. What could be causing this 400 behavior during the handshake? I suspect it might be related to the `proxy_pass` directive but I'm unsure how to proceed. Any guidance would be appreciated! Any help would be greatly appreciated!