1. 程式人生 > >HaProxy介紹,安裝及配置

HaProxy介紹,安裝及配置

替換 list 輪詢 info 相關配置 服務器 round 架構 啟用

1,HaProxy簡介
HAProxy 是一款可靠的,提供高可用性、負載均衡以及基於TCP(第四層)和HTTP(第七層)應用的代理軟件,支持虛擬主機,它是免費、快速並且可靠的一種解決方案。 HAProxy特別適用於那些負載特大的web站點,這些站點通常又需要會話保持或七層處理。HAProxy運行在時下的硬件上,完全可以支持數以萬計的 並發連接。並且它的運行模式使得它可以很簡單安全的整合進您當前的架構中, 同時可以保護你的web服務器不被暴露到網絡上。

2,安裝配置HaProxy
以下實驗環境均為CentOS7.3 x86_64平臺
技術分享圖片
因為我已經安裝過了,下面我主要講解下配置文件的內容,HaProxy的配置文件如下,

vim /etc/haproxy/haproxy.cfg
技術分享圖片
HaProxy的配置文件主要分為二個大的部分五個段
配置文件格式:
HAProxy的配置處理3類來主要參數來源:
——最優先處理的命令行參數;
——global配置段,用於設定全局配置參數;
——proxy相關配置段,如defaults、listen、frontend和backend;
全局配置
global

# 設置日誌文件輸出定向
log 127.0.0.1 local3 info

# 改變當前工作目錄
chroot /usr/local/haproxy

# 用戶與用戶組
user haproxy
group haproxy

# 守護進程啟動,運維方式為後臺工作
daemon

# 最大連接數
maxconn 4000

#作用於其後緊跟的listen塊,直至下一個defaults 塊,下一個default 將替換上一個塊作用於以後的listen
defaults

# 啟用每個實例日誌記錄事件和流量。
log global

# 默認的模式mode { tcp|http|health },tcp是4層,http是7層,health只會返回OK
mode http

# maxconn 65535         maxconn 每個進程可用的最大連接數
# retries 3         當對server的connection失敗後,重試的次數  
# option abortonclose     啟用或禁用在隊列中掛起的中止請求的早期丟棄 
# option redispatch     啟用或禁用在連接故障情況下的會話重新分配 
# option dontlognull     啟用和禁用 記錄 空連接
# option httpclose         每次請求完畢後主動關閉http通道,HA-Proxy不支持keep-alive模式 
# option forwardfor     獲得客戶端IP 
# option httplog        記錄HTTP 請求,session 狀態和計時器 
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000

代理相關配置
#前端配置,http_front名稱可自定義
frontend http_front

# bind *:443 ssl crt /etc/haproxy/cert.pem        啟用ssl證書 
# bind *:80                        發起http請求道80端口,會被轉發到設置的ip及端口
bind *:80

#haproxy的狀態管理頁面,通過/haproxy?stats來訪問
stats uri /haproxy?stats
default_backend http_back

#後端配置,http_back名稱可自定義
backend http_back

# 負載均衡方式
# source 根據請求源IP
# static-rr 根據權重
# leastconn 最少連接者先處理
# uri 根據請求的uri
# url_param 根據請求的url參數
# rdp-cookie 據據cookie(name)來鎖定並哈希每一次請求
# hdr(name) 根據HTTP請求頭來鎖定每一次HTTP請求
# roundrobin 輪詢方式
balance roundrobin

#設置健康檢查頁面
option httpchk GET /index.html

#傳遞客戶端真實IP
option forwardfor header X-Forwarded-For

# inter 2000 健康檢查時間間隔2秒
# rise 3 檢測多少次才認為是正常的
# fall 3 失敗多少次才認為是不可用的
# weight 30 權重
# 需要轉發的ip及端口
server node1 192.168.179.131:8081 check inter 2000 rise 3 fall 3 weight 30
server node2 192.168.179.131:8082 check inter 2000 rise 3 fall 3 weight 30
##############################################################
# haproxy的acl規則
frontend http_front
bind *:80
stats uri /haproxy?stats

#創建一個acl,is_http_back2是acl的名稱,可自定義,用於判斷主機名是否為www.back2.com
acl is_http_back2 hdr_end(host) www.back2.com

#通過正則判斷主機名中是否為bbs.back.com或forum.back.com
acl is_host_bbs hdr_reg(host) -i ^(bbs.back.com|forum.back.com)

#判斷ua是否為android
acl is_ua_android hdr_reg(User-Agent) -i android

#判斷主機名開頭是否為img.或css.或js.
acl is_host_static hdr_beg(host) -i img. css. js.

#判斷url路徑中是否有/bbs
acl is_path_bbs path_beg -i /bbs

#判斷url文件結尾
acl is_php path_end -i .php

#通過正則判斷url中結尾以
acl is_static_file url_reg -i /*.(css|jpg|png|jpeg|gif)$

#效果同上
acl is_static_file2 path_end -i .css .jpg .png .jpeg .gif

#如果主機名是www.back2.com那麽就使用後端http_back2
use_backend http_back2ifis_http_back2

#默認使用的後端
default_backend http_back

backend http_back
balance roundrobin
option httpchk GET /index.html
option forwardfor header X-Forwarded-For
server node1 192.168.1.222:8080 check inter 2000 rise 3 fall 3 weight 30

backend http_back2
balance roundrobin
option httpchk GET /index.html
option forwardfor header X-Forwarded-For
server node2 192.168.1.222:8082 check inter 2000 rise 3 fall 3 weight 30

#最後可以通過訪問配置HaProxy的管理頁面看到你配置的前端和後端的詳細情況
技術分享圖片

HaProxy介紹,安裝及配置