[root@Tang-1~]# cat /data/web/neo/index.html
<h1>This is neo's website!</h1>[root@Tang-1~]# cat /data/web/tang/index.html
<h1>This is tang's website!</h1>
2.3 HTTP 虚拟主机的服务启动及网页测试
[root@Tang-1~]# systemctl start httpd # 启动服务
[root@Tang-1~]# ss -tnl # 查看端口号是否正常启动
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN0128*:22*:*LISTEN0100127.0.0.1:25*:*LISTEN0128:::80:::*LISTEN0128:::8080:::*LISTEN0128:::22:::*LISTEN0100::1:25:::*
[root@Tang haproxy]# curl http://172.16.141.209:80 # 访问虚拟主机的默认网页
<h1>This is tang's website!</h1>[root@Tang haproxy]# curl http://172.16.141.209:8080 # 访问虚拟主机的默认网页
<h1>This is neo's website!</h1>
3、HAProxy 主机配置(代理配置)
3.1 HAProxy 主机的 haproxy.cfg 文件配置
[root@Tang ~]# ipinfo
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.141.252 netmask 255.255.255.0 broadcast 172.16.141.255[root@Tang haproxy]# vim /etc/haproxy/haproxy.cfg
frontend web
bind *:80
default_backend websrvs
backend websrvs
balance roundrobin
server srv1 172.16.141.209:80 weight 1 check
server srv2 172.16.141.209:8080 weight 2 check
3.2 HAProxy 主机的服务启动
[root@Tang haproxy]# systemctl start haproxy
[root@Tang haproxy]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN0128*:80*:*LISTEN0128*:22*:*LISTEN0100127.0.0.1:25*:*LISTEN0128:::22:::*
4、HAProxy 主机配置(log 配置)
4.1 HAProxy 主机的 haproxy.cfg 文件查看
[root@Tang haproxy]# vim /etc/haproxy/haproxy.cfg
global
# to have these messages end up in/var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONSin
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file.A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.*/var/log/haproxy.log
#
log 127.0.0.1 local2
[root@Tang-2~]# for i in{1..12};do curl http://172.16.141.252; done
<h1>This is tang's website!</h1><h1>This is neo's website!</h1><h1>This is neo's website!</h1><h1>This is tang's website!</h1><h1>This is neo's website!</h1><h1>This is tang's website!</h1><h1>This is neo's website!</h1><h1>This is neo's website!</h1><h1>This is tang's website!</h1><h1>This is neo's website!</h1><h1>This is tang's website!</h1><h1>This is neo's website!</h1>
5.2 查看 HAProxy 的相关日志
[root@Tang ~]# tail /var/log/haproxy/haproxy.log
Nov 614:47:22 neo haproxy[30442]:172.16.141.253:37450[06/Nov/2019:14:47:22.692] web websrvs/srv2 0/0/0/1/1200292------1/1/0/0/00/0"GET / HTTP/1.1"
Nov 614:47:22 neo haproxy[30442]:172.16.141.253:37452[06/Nov/2019:14:47:22.705] web websrvs/srv1 0/0/0/1/1200293------1/1/0/0/00/0"GET / HTTP/1.1"
Nov 614:47:22 neo haproxy[30442]:172.16.141.253:37454[06/Nov/2019:14:47:22.719] web websrvs/srv2 0/0/0/1/1200292------1/1/0/0/00/0"GET / HTTP/1.1"
Nov 614:47:22 neo haproxy[30442]:172.16.141.253:37456[06/Nov/2019:14:47:22.732] web websrvs/srv1 0/0/0/1/1200293------1/1/0/0/00/0"GET / HTTP/1.1"
Nov 614:47:22 neo haproxy[30442]:172.16.141.253:37458[06/Nov/2019:14:47:22.744] web websrvs/srv2 0/0/0/1/1200292------1/1/0/0/00/0"GET / HTTP/1.1"
Nov 614:47:22 neo haproxy[30442]:172.16.141.253:37460[06/Nov/2019:14:47:22.757] web websrvs/srv2 0/0/0/1/1200292------1/1/0/0/00/0"GET / HTTP/1.1"
Nov 614:47:22 neo haproxy[30442]:172.16.141.253:37462[06/Nov/2019:14:47:22.770] web websrvs/srv1 0/0/0/1/1200293------1/1/0/0/00/0"GET / HTTP/1.1"
Nov 614:47:22 neo haproxy[30442]:172.16.141.253:37464[06/Nov/2019:14:47:22.783] web websrvs/srv2 0/0/0/1/1200292------1/1/0/0/00/0"GET / HTTP/1.1"
Nov 614:47:22 neo haproxy[30442]:172.16.141.253:37466[06/Nov/2019:14:47:22.796] web websrvs/srv1 0/0/0/1/1200293------1/1/0/0/00/0"GET / HTTP/1.1"
Nov 614:47:22 neo haproxy[30442]:172.16.141.253:37468[06/Nov/2019:14:47:22.809] web websrvs/srv2 0/0/0/1/1200292------1/1/0/0/00/0"GET / HTTP/1.1"