Linux

haproxy 虛擬主機問題

  • May 25, 2011

此配置旨在將人們發送到 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。

我通過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 ..

如果

來自http://anismiles.wordpress.com/2011/01/25/websocket-and-node-js-why-shud%E2%80%99ya-care/ 您可以嘗試使用標頭升級和連接

# 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

引用自:https://serverfault.com/questions/272946