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

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

问题描述

我们正在开发一个需要利用 html5 websockets 的 Ruby on Rails 应用程序.目前,我们有两个独立的服务器":我们的主应用程序运行在 nginx+passenger 上,一个单独的服务器使用 Pratik Naik 的 Cramp 框架(在 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 配置并不是那么精通,那么,是否有可能将 nginx 配置为 websocket 服务器的反向代理?或者我必须等待 nginx 为新的 websocket 握手提供支持吗?假设需要同时让应用服务器和 websocket 服务器侦听端口 80,这是否意味着我现在必须在没有 nginx 的单独服务器上运行 Thin?

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天全站免登陆