Haproxy配置文件詳解
在前面的話,《Haproxy配置文件詳解》文檔部分信息來自網絡,同時參考過官方的架構指南,在此非常感謝zero提供的文檔,以及在學習Haproxy過程中的幫助。
#/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代表權重
以上為基本的配置文件,下面會對這個配置一一說明和應用。
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!