haproxy 虚拟主机问题

haproxy 虚拟主机问题

此配置旨在将人们发送到 Node,除非它是 .gif、.jpg 等,但它似乎不起作用(它总是将他们发送到 Nginx):

frontend all 0.0.0.0:80
  timeout client 86400000

  # ... unless they're on websockets, which Nginx can't handle
  acl is_websocket hdr(Upgrade) -i WebSocket
  acl is_websocket hdr_beg(Host) -i ws
  # static assets
  acl url_static path_end .jpg .jpeg .gif .png .ico .pdf .js .css .flv .swf
  acl is_domain hdr_end(host) -i SUB.DOMAIN.com
  # ... or are using Socket.io, which is served by node
  acl is_websocket path_beg /socket.io

  # conditional for hitting node
  use_backend node_backend  if !url_static is_domain
  use_backend nginx_backend if url_static

  # always send people to nginx
  default_backend nginx_backend

这是 haproxy 1.4。

编辑:我应该指出,我在与 Node.js 相同的域中提供静态资产,但通过 Nginx。因此,两种情况下的主机名都相同,我只是想将静态资产的请求转发到 Nginx。

答案1

如果

http://anismiles.wordpress.com/2011/01/25/websocket-and-node-js-why-shud%E2%80%99ya-care/ 您可以尝试使用 Header Upgrade 和 Connection

 # if header Upgrade = WebSocket and Connection=Upgrader
 acl is_websocket hdr(Upgrade) -i WebSocket AND hdr(Connection) -i Upgrade
 use_backend node_backend  if is_websocket

 # else
 default_backend nginx_backend

答案2

我通过option httpclose在后端添加内容解决了这个问题。似乎隐式持有的连接最终落入了 Nginx。我在这里找到了答案的线索

backend node_backend
  # node.js
  option forwardfor # This sets X-Forwarded-For
  option httpclose
  # .. etc ..


backend nginx_backend
  # nginx
  option forwardfor
  option httpclose
  # .. etc ..

相关内容