02 代理服务器及haproxy基础


#安装程序

[root@node1 ~]# yum -y install haproxy


#配置文件

[root@node1 ~]# cd /etc/haproxy/

[root@node1 haproxy]# cp haproxy.cfg{,.bak}

[root@node1 haproxy]# vim haproxy.cfg

修改frontend段的内容为

frontend  main *:80

default_backend             websrvs

修改backend段的内容为

backend websrvs

balance     roundrobin

server  web1    192.168.1.132:80 check

server  web2    192.168.1.133:80 check

其后面的内容全部删除

[root@node1 haproxy]# systemctl start haproxy.service


[root@node2 ~]# yum -y install httpd

[root@node2 ~]# echo "<h1>Web1</h1>" > /var/www/html/index.html

[root@node2 ~]# systemctl start httpd.service 


[root@node3 ~]# yum -y install httpd

[root@node3 ~]# echo "<h1>Web2</h1>" > /var/www/html/index.html 

[root@node3 ~]# systemctl start httpd.service


#测试

[root@node3 ~]# systemctl stop httpd.service 


#修改日志配置文件 

[root@node1 haproxy]# vim /etc/rsyslog.conf 

去掉

#$ModLoad imudp

#$UDPServerRun 514

前面的#号

添加

local2.*                                                /var/log/haproxy.log


[root@node1 haproxy]# systemctl restart rsyslog.service 


03 haproxy配置参数详解


算法测试

1、修改算法为source

[root@node1 haproxy]# vim haproxy.cfg

修改

balance     roundrobin

balance     source

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

2、修改算法为uri

[root@node1 haproxy]# vim haproxy.cfg

修改 

balance     source

balance     uri

hash-type   consistent

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

[root@node1 haproxy]# for i in {1..10};do echo "<h1>Page $i on Web</h1>" > /var/www/html/test$i.html;done

[root@node2 ~]# for i in {1..10};do echo "<h1>Page $i on Web1</h1>" > /var/www/html/test$i.html;done

[root@node3 ~]# for i in {1..10};do echo "<h1>Page $i on Web2</h1>" > /var/www/html/test$i.html;done 

3、修改算法为hdr

[root@node1 haproxy]# vim haproxy.cfg

修改 

balance     uri

balance     hdr(User-Agent)

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

4、使用bind参数

[root@node1 haproxy]# vim haproxy.cfg

修改 

frontend  main *:80

frontend  main 

bind *:80

bind *:8080

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

5、重定向

[root@node1 haproxy]# vim haproxy.cfg

修改backend段的内容为 

backend websrvs

balance     roundrobin

server  web1    192.168.1.133:80 check weight 1

server  web2    192.168.1.132:80 check weight 3

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

04 haproxy配置详解


1、配置cookie

[root@node1 haproxy]# vim haproxy.cfg

修改backend段的内容为:

backend websrvs

balance     roundrobin

cookie SERVERID insert nocache indirect

server  web1    192.168.1.133:80 check weight 1 cookie websrv1

server  web2    192.168.1.132:80 check weight 3 cookie websrv2

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

2、启用stats功能

[root@node1 haproxy]# vim haproxy.cfg

在frontend段后添加

listen statistics

bind *:9090

stats enable

stats hide-version

[root@node1 haproxy]# systemctl restart haproxy.service 

访问地址:

http://192.168.1.131:9090/haproxy?stats 

3、对stats添加认证功能

[root@node1 haproxy]# vim haproxy.cfg

在stats段后添加

stats uri /haproxyadmin?stats

stats realm "HAProxy Statistics"

stats auth admin:mageedu

stats admin if TRUE

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

访问地址:

http://192.168.1.131:9090/haproxyadmin?stats 

4、使用forwardfor参数(记录真实的客户端地址)

[root@node1 haproxy]# vim haproxy.cfg

在frontend段后添加

option forwardfor except 127.0.0.1

[root@node3 haproxy]# vim /etc/httpd/conf/httpd.conf 

修改 

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

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

[root@node3 ~]# systemctl restart httpd.service

5、自定义响应或请求报文首部

[root@node1 haproxy]# vim haproxy.cfg

在frontend段中添加

rspadd Via:\ node1

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