之前介紹過haproxy可以當作load balance,當現在有個需求是在某子網域的website還不需要做load balance,必須將連至該website的連線都導去指定的server上,本篇將實作透過haproxy acl的方式來完成此需求。
設定檔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | global
# maximum number of simultaneous active connections from an upstream web server
maxconn 32768
# 運行haproxy使用者及群組
user nobody
group nobody
# 存放運行haproxy的process id 檔案
pidfile /var/run/haproxy.pid
# 在背後運行haproxy
daemon
defaults
#Define the load balancing algorithm to be used in a backend
balance roundrobin
#Set the running mode or protocol of the instance
mode http
#重試次數
retries 3
#Enable or disable session redistribution in case of connection failure
option redispatch
#Enable or disable passive HTTP connection closing
option httpclose
# Enable or disable forced persistence on down servers
option persist
#Enable insertion of the X-Forwarded-For header to requests sent to servers
option forwardfor
#Set the maximum time to wait for a connection attempt to a server to succeed.
contimeout 5000
#Set the maximum inactivity time on the client side.
clitimeout 50000
#Set the maximum inactivity time on the server side.
srvtimeout 50000
# Enable the statistics page
stats enable
stats auth user:passwd
stats uri /haproxy?stats
stats realm Haproxy\ Statistics
stats auth haproxy:stats
frontend http-in
bind 192.168.1.203:80
acl jquery hdr_beg(host) -i jquery
acl blog hdr_beg(host) -i blog
use_backend jquery if jquery
use_backend blog if blog
#use_backend www_domain_com if is_www_domain_com
#default_backend www_example_com
backend jquery
server 192.168.1.200 192.168.1.200:80 cookie Server1 weight 5 check
server 192.168.1.201 192.168.1.201:80 cookie Server2 weight 5 check
server 192.168.1.202 192.168.1.202:80 cookie Server3 weight 5 check
backend blog
server 192.168.1.200 192.168.1.200:80 cookie Server1 weight 5 check |
說明
frontend:
section describes a set of listening sockets accepting client connections。
backend:
section describes a set of servers to which the proxy will connect to forward incoming connections。
hdr_beg(header):
Returns true when one of the headers begins with one of the strings。
use_backend:
Switch to a specific backend if/unless a Layer 7 condition is matched。
簡單說就是透過frontend當做前端過濾(acl)判斷,然後在依判斷將連線導致相對應(use_backend)的backend。主要是利用haproxy acl 的方式來判斷並指定相對應的server,透過hdr_beg(host)可以得到子網域的字串,然後經過判斷將該連接導去use_backend所指定的SERVER位置上。
參考資料
- http://haproxy.1wt.eu/#docs
- http://chiahu.com/blog/?p=532
- http://www.pigo.idv.tw/archives/448#more-448