# 沒有定義後端主機權重之前以輪詢的方式響應
[root@centos7 ~]# for i in {0..9};do curl http://10.1.252.153/index.html;done
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
# 定義主機權重之後,可以看到權重大的響應次數多
[root@centos7 ~]# for i in {0..9};do curl http://10.1.252.153/index.html;done
<h1>Backend Server 1</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
5.option httpchk
★文法:
option httpchk
option httpchk <uri>
option httpchk <method> <uri>
option httpchk <method> <uri> <version>
⊙作用:
基于http協定作7層(應用層)健康狀态檢測機制;
⊙請求封包的起始行:
<method> <uri> <version>
Examples :
# Relay HTTPS traffic to Apache instance and check service availability
# using HTTP request "OPTIONS * HTTP/1.1" on port 80.backend https_relay
mode tcp
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
server apache1 192.168.1.1:443 check port 80
★http-check expect [!] <match> <pattern>:
定義做健康狀态檢測後傳回的響應碼
⊙格式:
<match>:status(響應嗎)或string;
<pattern>:比對期望的響應結果的模式;
# only accept status 200 as valid
http-check expect status 200
log global
log 127.0.0.1:514 local0 notice # only send important events
log 127.0.0.1:514 local0 notice notice # same but limit output level
log ${LOCAL_SYSLOG}:514 local0 notice # send to local server
★capture request header <name> len <length>
記錄請求封包中的指定的首部的值于日志中;len用于指定要記錄的資訊的長度;
⊙範圍:frontend和listen
Example:
capture request header Host len 15
capture request header X-Forwarded-For len 15
capture request header Referer len 15 # 從哪裡跳轉而來
★capture response header <name> len <length>
記錄響應封包中的指定的首部的值于日志中;len用于指定要記錄的資訊的長度;
capture response header Content-length len 9
capture response header Location len 15
9.錯誤頁面自定義:
★errorfile <code> <file>
傳回由haproxy自定義的錯誤頁面
⊙參數:
<code> :is the HTTP status code. Currently, HAProxy is capable of generating codes 200, 400, 403, 408, 500, 502, 503, and 504.
<file> :designates a file containing the full HTTP response.