本文系轉載,方便之後查看,轉載請標明原地址:html
http://itnihao.blog.51cto.com/1741976/915537nginx
下面是正題:web
#/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -st `cat /var/run/haproxy.pid` ####################全局配置信息######################## #######參數是進程級的,一般和操做系統(OS)相關######### global maxconn 20480 #默認最大鏈接數 log 127.0.0.1 local3 #[err warning info debug] chroot /var/haproxy #chroot運行的路徑 uid 99 #所屬運行的用戶uid gid 99 #所屬運行的用戶組 daemon #之後臺形式運行haproxy nbproc 1 #進程數量(能夠設置多個進程提升性能) pidfile /var/run/haproxy.pid #haproxy的pid存放路徑,啓動進程的用戶必須有權限訪問此文件 ulimit-n 65535 #ulimit的數量限制 #####################默認的全局設置###################### ##這些參數能夠被利用配置到frontend,backend,listen組件## defaults log global mode http #所處理的類別 (#7層 http;4層tcp ) maxconn 20480 #最大鏈接數 option httplog #日誌類別http日誌格式 option httpclose #每次請求完畢後主動關閉http通道 option dontlognull #不記錄健康檢查的日誌信息 option forwardfor #若是後端服務器須要得到客戶端真實ip須要配置的參數,能夠從Http Header中得到客戶端ip option redispatch #serverId對應的服務器掛掉後,強制定向到其餘健康的服務器 option abortonclose #當服務器負載很高的時候,自動結束掉當前隊列處理比較久的鏈接 stats refresh 30 #統計頁面刷新間隔 retries 3 #3次鏈接失敗就認爲服務不可用,也能夠經過後面設置 balance roundrobin #默認的負載均衡的方式,輪詢方式 #balance source #默認的負載均衡的方式,相似nginx的ip_hash #balance leastconn #默認的負載均衡的方式,最小鏈接 contimeout 5000 #鏈接超時 clitimeout 50000 #客戶端超時 srvtimeout 50000 #服務器超時 timeout check 2000 #心跳檢測超時 ####################監控頁面的設置####################### listen admin_status #Frontend和Backend的組合體,監控組的名稱,按需自定義名稱 bind 0.0.0.0:65532 #監聽端口 mode http #http的7層模式 log 127.0.0.1 local3 err #錯誤日誌記錄 stats refresh 5s #每隔5秒自動刷新監控頁面 stats uri /admin?stats #監控頁面的url stats realm itnihao\ itnihao #監控頁面的提示信息 stats auth admin:admin #監控頁面的用戶和密碼admin,能夠設置多個用戶名 stats auth admin1:admin1 #監控頁面的用戶和密碼admin1 stats hide-version #隱藏統計頁面上的HAproxy版本信息 stats admin if TRUE #手工啓用/禁用,後端服務器(haproxy-1.4.9之後版本) errorfile 403 /etc/haproxy/errorfiles/403.http errorfile 500 /etc/haproxy/errorfiles/500.http errorfile 502 /etc/haproxy/errorfiles/502.http errorfile 503 /etc/haproxy/errorfiles/503.http errorfile 504 /etc/haproxy/errorfiles/504.http #################HAProxy的日誌記錄內容設置################### capture request header Host len 40 capture request header Content-Length len 10 capture request header Referer len 200 capture response header Server len 40 capture response header Content-Length len 10 capture response header Cache-Control len 8 #######################網站監測listen配置##################### ###########此用法主要是監控haproxy後端服務器的監控狀態############ listen site_status bind 0.0.0.0:1081 #監聽端口 mode http #http的7層模式 log 127.0.0.1 local3 err #[err warning info debug] monitor-uri /site_status #網站健康檢測URL,用來檢測HAProxy管理的網站是否能夠用,正常返回200,不正常返回503 acl site_dead nbsrv(server_web) lt 2 #定義網站down時的策略當掛在負載均衡上的指定backend的中有效機器數小於1臺時返回true acl site_dead nbsrv(server_blog) lt 2 acl site_dead nbsrv(server_bbs) lt 2 monitor fail if site_dead #當知足策略的時候返回503,網上文檔說的是500,實際測試爲503 monitor-net 192.168.16.2/32 #來自192.168.16.2的日誌信息不會被記錄和轉發 monitor-net 192.168.16.3/32 ########frontend配置############ #####注意,frontend配置裏面能夠定義多個acl進行匹配操做######## frontend http_80_in bind 0.0.0.0:80 #監聽端口,即haproxy提供web服務的端口,和lvs的vip端口相似 mode http #http的7層模式 log global #應用全局的日誌配置 option httplog #啓用http的log option httpclose #每次請求完畢後主動關閉http通道,HA-Proxy不支持keep-alive模式 option forwardfor #若是後端服務器須要得到客戶端的真實IP須要配置次參數,將能夠從Http Header中得到客戶端IP ########acl策略配置############# acl itnihao_web hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$ #若是請求的域名知足正則表達式中的2個域名返回true -i是忽略大小寫 acl itnihao_blog hdr_dom(host) -i blog.itnihao.cn #若是請求的域名知足www.itnihao.cn返回true -i是忽略大小寫 #acl itnihao hdr(host) -i itnihao.cn #若是請求的域名知足itnihao.cn返回true -i是忽略大小寫 #acl file_req url_sub -i killall= #在請求url中包含killall=,則此控制策略返回true,不然爲false #acl dir_req url_dir -i allow #在請求url中存在allow做爲部分地址路徑,則此控制策略返回true,不然返回false #acl missing_cl hdr_cnt(Content-length) eq 0 #當請求的header中Content-length等於0時返回true ########acl策略匹配相應############# #block if missing_cl #當請求中header中Content-length等於0阻止請求返回403 #block if !file_req || dir_req #block表示阻止請求,返回403錯誤,當前表示若是不知足策略file_req,或者知足策略dir_req,則阻止請求 use_backend server_web if itnihao_web #當知足itnihao_web的策略時使用server_web的backend use_backend server_blog if itnihao_blog #當知足itnihao_blog的策略時使用server_blog的backend #redirect prefix http://blog.itniaho.cn code 301 if itnihao #當訪問itnihao.cn的時候,用http的301挑轉到http://192.168.16.3 default_backend server_bbs #以上都不知足的時候使用默認server_bbs的backend ##########backend的設置############## #下面我將設置三組服務器 server_web,server_blog,server_bbs ###########################backend server_web############################# backend server_web mode http #http的7層模式 balance roundrobin #負載均衡的方式,roundrobin平均方式 cookie SERVERID #容許插入serverid到cookie中,serverid後面能夠定義 option httpchk GET /index.html #心跳檢測的文件 server web1 192.168.16.2:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1 #服務器定義,cookie 1表示serverid爲web1,check inter 1500是檢測心跳頻率rise 3是3次正確認爲服務器可用, #fall 3是3次失敗認爲服務器不可用,weight表明權重 server web2 192.168.16.3:80 cookie web2 check inter 1500 rise 3 fall 3 weight 2 #服務器定義,cookie 1表示serverid爲web2,check inter 1500是檢測心跳頻率rise 3是3次正確認爲服務器可用, #fall 3是3次失敗認爲服務器不可用,weight表明權重 ###################################backend server_blog############################################### backend server_blog mode http #http的7層模式 balance roundrobin #負載均衡的方式,roundrobin平均方式 cookie SERVERID #容許插入serverid到cookie中,serverid後面能夠定義 option httpchk GET /index.html #心跳檢測的文件 server blog1 192.168.16.2:80 cookie blog1 check inter 1500 rise 3 fall 3 weight 1 #服務器定義,cookie 1表示serverid爲web1,check inter 1500是檢測心跳頻率rise 3是3次正確認爲服務器可用,fall 3是3次失敗認爲服務器不可用,weight表明權重 server blog2 192.168.16.3:80 cookie blog2 check inter 1500 rise 3 fall 3 weight 2 #服務器定義,cookie 1表示serverid爲web2,check inter 1500是檢測心跳頻率rise 3是3次正確認爲服務器可用,fall 3是3次失敗認爲服務器不可用,weight表明權重 ###################################backend server_bbs############################################### backend server_bbs mode http #http的7層模式 balance roundrobin #負載均衡的方式,roundrobin平均方式 cookie SERVERID #容許插入serverid到cookie中,serverid後面能夠定義 option httpchk GET /index.html #心跳檢測的文件 server bbs1 192.168.16.2:80 cookie bbs1 check inter 1500 rise 3 fall 3 weight 1 #服務器定義,cookie 1表示serverid爲web1,check inter 1500是檢測心跳頻率rise 3是3次正確認爲服務器可用,fall 3是3次失敗認爲服務器不可用,weight表明權重 server bbs2 192.168.16.3:80 cookie bbs2 check inter 1500 rise 3 fall 3 weight 2 #服務器定義,cookie 1表示serverid爲web2,check inter 1500是檢測心跳頻率rise 3是3次正確認爲服務器可用,fall 3是3次失敗認爲服務器不可用,weight表明權重