1. 程式人生 > >Haproxy的配置

Haproxy的配置

tails 127.0.0.1 warn ges 網站 比較 back cnblogs mit

1,下載Haproxy

下載Haproxy 1.6 技術分享

2,安裝haproxy

uname -r

技術分享

cd /usr/local/src/haproxy-1.6.9/

make TARGET=linux3100 ARCH=x86_64 USE_OPENSSL=1 ADDLIB=-lz

技術分享

make install PREFIX=/usr/local/haproxy

技術分享

配置haproxy

創建這三個目錄

技術分享

配置系統日誌

vim /etc/rsyslog.conf

技術分享

[[email protected] haproxy]# vim /etc/rsyslog.conf
[[email protected]

/* */ haproxy]# cat /etc/sysconfig/rsyslog
# Options for rsyslogd
# Syslogd options are deprecated since rsyslog v3.
# If you want to use them, switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for more details
SYSLOGD_OPTIONS="-r -m 0 -c 2"

5,重啟rsyslog

systemctl restart rsyslog

6,配置Haproxy cfg文件

###########全局配置#########
global
chroot /usr/local/haproxy
daemon
nbproc 1
group haproxy
user haproxy
pidfile /usr/local/haproxy/logs/haproxy.pid
ulimit-n 65536
tune.ssl.default-dh-param 2048
log 127.0.0.1 local0
#spread-checks 5m
#stats timeout 5m
#stats maxconn 100

########默認配置############
defaults
mode tcp #默認的模式mode { tcp|http|health },tcp是4層,http是7層,health只會返回OK
retries 3 #兩次連接失敗就認為是服務器不可用,也可以通過後面設置
option redispatch #當serverId對應的服務器掛掉後,強制定向到其他健康的服務器
option abortonclose #當服務器負載很高的時候,自動結束掉當前隊列處理比較久的鏈接
maxconn 32000 #默認的最大連接數
timeout connect 5000ms #連接超時
timeout client 30000ms #客戶端超時
timeout server 30000ms #服務器超時
#timeout check 2000 #心跳檢測超時
log 127.0.0.1 local0 err #[err warning info debug]

listen admin_status #Frontend和Backend的組合體,監控組的名稱,按需自定義名稱
bind 0.0.0.0:1314 #監聽端口
mode http #http的7層模式
log 127.0.0.1 local0 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後端服務器的監控狀態############
frontend http_80_in
bind 0.0.0.0:80 #監聽端口,即haproxy提供web服務的端口,和lvs的vip端口類似
bind *:443 ssl crt /usr/local/haproxy/server.pem
redirect scheme https if !{ ssl_fc }
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 haproxy.duobaonet.com
use_backend server_web if itnihao_web
#下面我將設置三組服務器 server_web,server_blog,server_bbs
###########################backend server_web#############################
backend server_web
mode http #http的7層模式
balance roundrobin #負載均衡的方式,roundrobin平均方式
cookie SERVERID #允許插入serverid到cookie中,serverid後面可以定義
server web1 127.0.0.1:51001 cookie web1 check inter 1500 rise 3 fall 3 weight 1

註意:設置haproxy代理後端tomcat 並設置https http自動跳轉到https

證書合並:cat 214207719200903.pem 214207719200903.key |tee server.pem

8,啟動Haproxy

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg

9,把haproxy加入系統服務。

Haproxy的配置