CodexBloom - Programming Q&A Platform

Trouble with Nginx Reverse Proxy Configuration on CentOS 8 for WebSocket Support

πŸ‘€ Views: 50 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-21
nginx websocket centos nodejs JavaScript

I've been researching this but I'm collaborating on a project where I'm currently trying to set up an Nginx reverse proxy on CentOS 8 to handle WebSocket connections for a Node.js application... While the regular HTTP requests go through seamlessly, the WebSocket connections unexpected result with a `101 Switching Protocols` behavior. I've added the necessary configurations to my `nginx.conf`, but it seems like the WebSocket upgrade isn't being handled properly. Here’s the relevant part of my Nginx configuration: ```nginx server { listen 80; server_name mydomain.com; location / { 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; } } ``` The Node.js server is running correctly on port 3000, and I'm able to access it directly without any issues. However, when I try to initiate a WebSocket connection from the client side, I get the following behavior in the browser console: ``` WebSocket connection to 'ws://mydomain.com/' failed: behavior during WebSocket handshake: Unexpected response code: 400 ``` I’ve double-checked my firewall settings and confirmed that port 80 is open. I've also tried adding the `proxy_set_header` lines for the WebSocket upgrade, but nothing seems to change. Is there something I’m overlooking here? Any help would be greatly appreciated! This is part of a larger API I'm building. I'd really appreciate any guidance on this. I'd really appreciate any guidance on this.