1. 程式人生 > >HAProxy雜記(2)

HAProxy雜記(2)

HAProxy 基礎

haproxy高級配置

haproxy cookie

1、配置插入cookie

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvs
    balance     roundrobin
    cookie SERVERID insert nocache indirect
    server web1 10.201.106.132:80 check weight 1 cookie websrv1
    server web2 10.201.106.133:80 check weight 3 cookie websrv2

重載服務:
[root@master1 haproxy]# systemctl reload haproxy.service

由於每次請求頭部的cookie都是websrv2字段,10個子網頁全部都是由web2響應;
技術分享圖片

狀態監控頁

1、配置文件配置

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#--
frontend  main
    bind *:80
    bind *:8080
    default_backend            websrvs

listen statistics
    bind *:9090
    stats enable
    stats hide-version
#    stats scope .

[root@master1 haproxy]# systemctl restart haproxy.service
[root@master1 haproxy]# ss -tnl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128           *:8080                      *:*                  
LISTEN      0      128           *:80                        *:*                  
LISTEN      0      128           *:22                        *:*                  
LISTEN      0      100    127.0.0.1:25                        *:*                  
LISTEN      0      128           *:9090                      *:*                  
LISTEN      0      128          :::22                       :::*                  
LISTEN      0      100         ::1:25                       :::*                  
[root@master1 haproxy]# 

訪問測試:http://10.201.106.131:9090/haproxy?stats
技術分享圖片

2、修改狀態頁路徑

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main
    bind *:80
    bind *:8080
    default_backend            websrvs

listen statistics
    bind *:9090
    stats enable
    stats hide-version
    stats scope .
    stats uri /haproxyadmin?stats       #修改狀態頁地址
    stats realm "HAPorxy\ Statistics"   #認證提醒   
    stats auth admin:mageedu            #賬號密碼

重載服務:[root@master1 haproxy]# systemctl reload haproxy.service

測試:再次登陸需要賬號密碼;

3、配置認證成功時,開啟管理接口

listen statistics
    bind *:9090
    stats enable
    stats hide-version
    #stats scope .                  #去掉當前有效
    stats uri /haproxyadmin?stats
    stats realm "HAPorxy\ Statistics"
    stats auth admin:mageedu
    stats admin if TRUE             #

option,記錄真實客戶端地址

1、默認在default開啟

2、修改後端httpd的配置文件

[root@master2 ~]# vim /etc/httpd/conf/httpd.conf 

    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

[root@master2 ~]# systemctl reload httpd.service

3、後端web查看日誌

[root@master2 ~]# tail /var/log/httpd/access_log 
10.201.106.1 - - [06/Feb/2017:11:02:11 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [06/Feb/2017:11:02:11 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [06/Feb/2017:11:02:11 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [06/Feb/2017:11:02:12 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [06/Feb/2017:11:02:12 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [06/Feb/2017:11:02:12 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [06/Feb/2017:11:02:12 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [06/Feb/2017:11:02:13 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [06/Feb/2017:11:02:13 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
10.201.106.1 - - [06/Feb/2017:11:02:13 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
[root@master2 ~]# 

響應報文首部修改

1、修改haproxy配置文件

frontend  main
    bind *:80
    bind *:8080
    rspadd Via:\ node1.magedu.com
    default_backend            websrvs

[root@master1 haproxy]# systemctl reload haproxy.service

技術分享圖片

HAProxy雜記(2)