CodexBloom - Programming Q&A Platform

How to implement guide with nginx reverse proxy configurations for websocket connections on centos 8

👀 Views: 0 💬 Answers: 1 📅 Created: 2025-06-15
nginx websocket centos8 Nginx

I'm sure I'm missing something obvious here, but Hey everyone, I'm running into an issue that's driving me crazy. I'm currently setting up an Nginx reverse proxy on a CentOS 8 server to handle WebSocket connections for a real-time chat application. While standard HTTP requests are functioning properly, I'm working with issues with WebSocket connections failing to upgrade. The specific behavior I'm receiving in the browser console is 'WebSocket connection to 'ws://example.com/socket' failed: behavior during WebSocket handshake: Unexpected response code: 400'. My Nginx configuration looks like this: ```nginx server { listen 80; server_name example.com; location /socket { 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 have verified that the WebSocket server (running on port 3000) is operational and can accept connections directly. Additionally, I’ve checked the firewall settings to ensure that traffic on both ports 80 and 3000 is allowed. I also tried changing the `proxy_http_version` to 1.0, but that didn't help either. Is there something I'm missing in the Nginx configuration that would cause the WebSocket handshake to unexpected result? Any insights would be greatly appreciated. I'm working on a application that needs to handle this. What's the best practice here?