Haproxy

Haproxy位置

這裏寫圖片描述

配置段

global:全局配置段
進程及安全配置相關的參數
性能調整相關參數
Debug參數
proxies:代理配置段
defaults:爲frontend, backend, listen提供默認配置
fronted:前端,相當於nginx, server {}
backend:後端,相當於nginx, upstream {}
listen:同時擁有前端和後端,適用於一對一環境

示例:

frontend web
    bind *:80
    default_backend    websers
backend websers
    server ser1 192.168.6.6:80 check
    server ser1 192.168.6.7:80 check

全局配置 global

進程及安全管理: chroot, deamon, user, group, uid, gid

nbproc :要啓動的haproxy的進程數量,系統默認單進程,要求使用daemon模式

ulimit-n :每個haproxy進程可打開的最大文件數,系統自動會指定,不建議設置daemon 後端方式運行,建議使用

log:定義全局的syslog服務器;最多可以定義兩個log<address> [len <length>] <facility> [max level[min level]]

address: rsyslog服務器地址
len: 記錄日誌的長度,默認1024

記錄日誌

# 默認發往本機的日誌服務器;
vi /etc/rsyslog.conf
(1) local2.*    /var/log/haproxy.log
(2) 去掉註釋
$ModLoad imudp
$UDPServerRun 514

遠端記錄日誌

# haproxy配置文件
log         遠端服務器IP  local2
# 發往遠端的日誌服務器;
vi /etc/rsyslog.conf
(1) local2.*    /var/log/haproxy.log
(2) 去掉註釋
$ModLoad imudp
$UDPServerRun 514

日誌管理

capture cookie <name> len <length>:捕獲請求和響應報文中的 cookie並記錄日誌

capture request header <name> len <length> :捕獲請求報文中指定的首部並記錄日誌

示例:

capture request header X-Forwarded-For len 15
capture response header <name> len <length>

捕獲響應報文中指定的首部並記錄日誌

示例:

capture response header Content-length len 9
capture response header Location len 15

性能調整:

  • maxconn <number>
    設定每個haproxy進程所能接受的最大併發連接數
  • maxconnrate <number>
    設置每個進程每秒種所能建立的最大連接數量
  • maxsessrate <number>
    設置每個進程每秒種所能建立的最大會話數量
  • maxsslconn <number>
    每進程支持SSL的最大連接數量
  • spread-checks <0..50, in percent>
    健康檢測延遲時長比建議2-5之間

代理配置段 proxies

defaults <name>
frontend <name>
backend <name>
listen <name>

配置參數
bind:指定一個或多個前端偵聽地址和端口
bind [<address>]:<port_range> [, ...] [param*]
balance:後端服務器組內的服務器調度算法
server <name> <address>[:[port]] [param*]
定義後端主機的各服務器及其選項
server <name> <address>[:port] [settings ...]
default-server [settings …] #爲backend中的各server設定默認選項
<name>:服務器在haproxy上的內部名稱;出現在日誌及警告信息
<address>:服務器地址,支持使用主機名
[:[port]]:端口映射;省略時,表示同bind中綁定的端口
[param*]:參數
weight :權重,默認爲1
maxconn :當前後端server的最大併發連接數
backlog :當server的連接數達到上限後的後援隊列長度
backup:設定當前server爲備用服務器Sorry Server
check:對當前server做健康狀態檢測,只用於四層檢測
注意: httpchk, “smtpchk”, “mysql-check”, “pgsqlcheck” and “ssl-hello-chk” 用於定義應用層檢測方法
addr :檢測時使用的IP地址
port :針對此端口進行檢測
inter <delay>:檢測之間的時間間隔,默認爲2000ms
rise <count>:連續多少次檢測結果爲“成功”才標記服務器爲可用;默認爲2
fall <count>:連續多少次檢測結果爲“失敗”才標記服務器爲不可用;默認爲3
disabled:標記爲不可用
redir <prefix>:將發往此server的所有GET和HEAD類的請求重定向至指定的URL

統計接口啓用相關的參數

stats enable
啓用統計頁;基於默認的參數啓用stats page
stats uri : /haproxy?stats uri #默認uri
stats uri <prefix>
自定義stats page uri,默認爲
stats auth <user>:<passwd>
認證時的賬號和密碼,可使用多次
stats realm <realm>
認證時的realm
stats hide-version
隱藏版本
stats refresh <delay>
設定自動刷新時間間隔
stats admin { if | unless } <cond>
啓用stats page中的管理功能
配置示例:

listen stats
bind :9999
stats enable #啓用統計功能
stats realm HAPorxy\ Stats\ Page
stats auth admin1:password1
stats auth admin1:password2
stats refresh 3s
stats admin if TRUE

工作模式

maxconn <conns>:爲指定的frontend定義其最大併發連接數;默認爲3000
fullconn <conns>:backend連接數
mode { tcp|http|health } #在default裏定義的默認是http
定義haproxy的工作模式
tcp:基於layer4實現代理;可代理mysql, pgsql, ssh,ssl等協議,https時使用此模式,默認模式
http:僅當代理協議爲http時使用,centos實際默認模式
health:工作爲健康狀態檢查的響應模式,當連接請求到達時應“OK”後即斷開連接,較少使用

錯誤頁配置

errorfile 自定義錯誤頁

<code>: HTTP status code.支持200, 400, 403, 408, 500, 502, 503, 504.
<file>:錯誤頁文件路徑

示例:

errorfile 400 /etc/haproxy/errorfiles/400badreq.http
errorfile 408 /dev/null # workaround Chrome preconnect bug
errorfile 403 /etc/haproxy/errorfiles/403forbid.http
errorfile 503 /etc/haproxy/errorfiles/503sorry.http

修改報文首部

在請求報文尾部添加指定首部

reqadd <string> [{if | unless} <cond>]

在響應報文尾部添加指定首部

rspadd <string> [{if | unless} <cond>]

示例:

rspadd X-Via:\ HAPorxy

從請求報文中刪除匹配正則表達式的首部

reqdel <search> [{if | unless} <cond>]
reqidel <search> [{if | unless} <cond>] 不分大小寫

從響應報文中刪除匹配正則表達式的首部

rspdel <search> [{if | unless} <cond>]
rspidel <search> [{if | unless} <cond>] 不分大小寫

示例:
rspidel server.*

配置HAProxy支持https協議

# 在前端加上以下3句
frontend  web
    bind 172.18.253.6:80
    redirect scheme https if ! { ssl_fc } #如果請求沒有https則重定向https
    bind 172.18.253.6:443 ssl crt /etc/haproxy/haproxy.pem
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章