Hey everyone,
I’m working on a project that uses Ruby on Rails and needs websockets. Right now we have two servers: our main app on nginx with Passenger and a separate websocket server using Cramp on Thin.
We want to put everything behind nginx so we don’t need different ports. But there’s a problem. When we try this, nginx seems to close the connections to Thin too quickly. It looks like nginx doesn’t know the client wants a long-running websocket connection.
I’m not great with nginx config. Can anyone tell me if it’s possible to set up nginx as a reverse proxy for websockets? Or do I have to wait for nginx to support the new websocket handshake?
If we need both servers on port 80, does that mean we have to run Thin on its own without nginx for now?
Any help would be awesome. Thanks!
hey FlyingEagle, yea nginx can handle websockets as a reverse proxy. u gotta use the ‘proxy_set_header’ directive for Upgrade and Connection headers. also set ‘proxy_http_version 1.1’. might wanna check ur timeouts too. it’s a bit tricky but totally doable. good luck with ur project!
Absolutely, nginx can function as a reverse proxy for WebSocket connections. The key is proper configuration. You’ll need to adjust your nginx.conf file to include specific WebSocket-friendly directives. Set ‘proxy_http_version 1.1’ and use ‘proxy_set_header’ for Upgrade and Connection headers. Also, increase ‘proxy_read_timeout’ to prevent premature connection closures. With these changes, nginx should correctly handle the WebSocket handshake and maintain long-lived connections. Remember to test thoroughly after implementation. If issues persist, examine your application’s WebSocket implementation for potential conflicts with the proxy setup.
ooh, websockets with nginx! sounds intriguing. have u tried tweaking the ‘proxy_read_timeout’ setting? it might help keep those connections alive longer. also, whats ur experience been like with Cramp? ive been curious about trying it out. any tips or gotchas to watch out for?