Nginx可以用作后端Websocket服务器的反向代理吗? [英] Can nginx be used as a reverse proxy for a backend websocket server?

查看:465
本文介绍了Nginx可以用作后端Websocket服务器的反向代理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个需要利用html5 websockets的Ruby on Rails应用程序.目前,我们可以说有两个单独的服务器":我们的主要应用程序运行在nginx + passenger上,一个单独的服务器使用Pratik Naik的 Thin 上运行a>)来处理websocket连接.

We're working on a Ruby on Rails app that needs to take advantage of html5 websockets. At the moment, we have two separate "servers" so to speak: our main app running on nginx+passenger, and a separate server using Pratik Naik's Cramp framework (which is running on Thin) to handle the websocket connections.

理想地,当需要进行部署时,我们将在nginx + passenger上运行rails应用,并且websocket服务器将被代理在nginx之后,因此我们不需要将websocket服务器运行在其他服务器上端口.

Ideally, when it comes time for deployment, we'd have the rails app running on nginx+passenger, and the websocket server would be proxied behind nginx, so we wouldn't need to have the websocket server running on a different port.

问题是,在此设置中,nginx似乎太早关闭了与Thin的连接.与瘦服务器的连接成功建立,然后立即用200响应代码关闭.我们的猜测是,nginx并未意识到客户端正在尝试为websocket流量建立长期运行的连接.

Problem is, in this setup it seems that nginx is closing the connections to Thin too early. The connection is successfully established to the Thin server, then immediately closed with a 200 response code. Our guess is that nginx doesn't realize that the client is trying to establish a long-running connection for websocket traffic.

诚然,我对nginx config并不那么精明,因此,甚至可以将nginx配置为充当websocket服务器的反向代理吗?还是我必须等待nginx为新的websocket握手内容提供支持?假设同时需要在端口80上监听应用程序服务器和Websocket服务器,这是否意味着我现在必须先在单独的服务器上运行Thin,而现在没有nginx在前面?

Admittedly, I'm not all that savvy with nginx config, so, is it even possible to configure nginx to act as a reverse proxy for a websocket server? Or do I have to wait for nginx to offer support for the new websocket handshake stuff? Assuming that having both the app server and the websocket server listening on port 80 is a requirement, might that mean I have to have Thin running on a separate server without nginx in front for now?

在此先感谢您的任何意见或建议. :)

Thanks in advance for any advice or suggestions. :)

-约翰

推荐答案

您目前不能为此使用nginx [已不复存在] ,但我建议您看一下HAProxy.我已经将它用于此目的.

You can't use nginx for this currently[it's not true anymore], but I would suggest looking at HAProxy. I have used it for exactly this purpose.

诀窍是设置较长的超时时间,以使套接字连接不会关闭.像这样:

The trick is to set long timeouts so that the socket connections are not closed. Something like:

timeout client  86400000 # In the frontend
timeout server  86400000 # In the backend

如果您想在同一端口上使用Rails和Cramp应用程序,则可以使用ACL规则来检测WebSocket连接并使用其他后端.因此,您的haproxy前端配置看起来像

If you want to serve say a rails and cramp application on the same port you can use ACL rules to detect a websocket connection and use a different backend. So your haproxy frontend config would look something like

frontend all 0.0.0.0:80
  timeout client    86400000
  default_backend   rails_backend
  acl websocket hdr(Upgrade)    -i WebSocket
  use_backend   cramp_backend   if websocket

为完整起见,后端看起来像

For completeness the backend would look like

backend cramp_backend
  timeout server  86400000
  server cramp1 localhost:8090 maxconn 200 check

这篇关于Nginx可以用作后端Websocket服务器的反向代理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆