haproxy基于一般的调度器设置(haproxy负载配置)
nanshan 2024-10-22 13:04 16 浏览 0 评论
基于一般的调度器设置(有listen,但无frontend和backend)
项目1:haproxy机器部署web集群(httpd是虚拟主机情况,只检查IP端口,不检查域名和站点网页)
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端能自动按照域名识别)
[root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg //只检查IP端口,不检查域名和站点网页
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen test
bind 172.10.10.5:80
mode http
stats enable
stats uri /admin?stats //web界面的uri,访问时格式:http://调度IP或域名/admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
option forwardfor //在web网站配置日志中能够看到客户端真实的IP,不好演示,做一个说明即可。
timeout server 15s
timeout connect 15s
option httpclose
cookie SERVERID rewrite
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
server web2 192.168.4.200:80 cookie B maxconn 4096 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP的设置
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common //记录客户端真实IP
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common //记录客户机真实IP
</VirtualHost>
wq
[root@localhost 桌面]# /etc/init.d/httpd restart
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo changyou1 > /changyou/index.html
[root@localhost 桌面]# echo tarena1 > /tarena/index.html
两台web服务器上配置:(192.168.4.200上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# /etc/init.d/httpd restart
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo changyou2 > /changyou/index.html
[root@localhost 桌面]# echo tarena2 > /tarena/index.html
客户端机器(172.10.10.6)
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena2
[root@localhost 桌面]# curl www.tarena.com
tarena2
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena2
做个说明:当后端的服务器的其中一个宕机后,会被haproxy自动剔除集群,不演示了,但看到效果了。
客户端访问调度器的web页面,查看后端web服务器是否正常:
输入:http://调度器IP或域名/admin?Stats如下图:
输入配置文件中设置的用户名和密码:admin,123456,如下图:(当后台两服务器都正常时)
当web1宕机后,如下图:
当web1恢复启动后,web界面又恢复正常,如下图:
项目2:haproxy机器部署web集群(httpd是虚拟主机情况,健康检查站点网页,当检查到站点有网页才认为正常)
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端能自动按照域名识别) [root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg //健康检查站点网页
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen test
bind 172.10.10.5:80
mode http
stats enable
stats uri /admin?stats //web界面的uri,访问时格式:http://调度IP或域名/admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
option forwardfor //在web网站配置日志中能够看到客户端真实的IP,不好演示,做一个说明即可。
option httpchk HEAD /check.html HTTP/1.0 //当站点目录有check.html才认为网站正常,HEAD也可换成GET
timeout server 15s
timeout connect 15s
option httpclose
cookie SERVERID rewrite
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
server web2 192.168.4.200:80 cookie B maxconn 4096 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上) //此时站点目录没有check.html,但两web服务都启动正常
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP的设置
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common //记录客户端真实IP
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common //记录客户端真实IP
</VirtualHost>
wq
[root@localhost 桌面]# /etc/init.d/httpd restart
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo changyou1 > /changyou/index.html
[root@localhost 桌面]# echo tarena1 > /tarena/index.html
两台web服务器上配置:(192.168.4.200上) //此时站点目录没有check.html,但两web服务都启动正常
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log //记录客户端真实IP
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# /etc/init.d/httpd restart
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo changyou2 > /changyou/index.html
[root@localhost 桌面]# echo tarena2 > /tarena/index.html
客户端机器(172.10.10.6)访问测试: //因为检查不到站点目录的check.html,所以认为不正常,访问不到
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com
[root@localhost 桌面]# curl www.changyou.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@localhost 桌面]# curl www.changyou.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@localhost 桌面]# curl www.tarena.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@localhost 桌面]# curl www.changyou.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
两台web服务器上配置:(192.168.4.100上) //此时站点目录有check.html
[root@localhost 桌面]# touch /changyou/check.html
[root@localhost 桌面]# touch /tarena/check.html
两台web服务器上配置:(192.168.4.200上) //此时站点目录有check.html
[root@localhost 桌面]# touch /changyou/check.html
[root@localhost 桌面]# touch /tarena/check.html
客户端机器(172.10.10.6)访问测试: //因为能检查到站点目录的check.html,所以认为正常,可以访问
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena2
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena2
[root@localhost 桌面]# curl www.tarena.com
tarena2
做个说明:当后端的服务器的其中一个宕机后,会被haproxy自动剔除集群,不演示了,但看到效果了。
客户端访问调度器的web页面,查看后端web服务器是否正常:
输入:http://调度器IP或域名/admin?Stats如下图:
输入配置文件中设置的用户名和密码:admin,123456,如下图:(当后台两服务器都正常时)
当web1宕机后或者web1正常,但是站点目录无check.html,如下图:
当web1恢复启动且站点目录有check.html后,web界面又恢复正常,如下图:
基于acl控制的调度器设置(无listen,但有frontend和backend)
项目1:基于定义域名acl规则,然后根据不同域名抛给后面对应服务器池中或跳转。
要求:访问www.chagnyou.com时,抛给后面的chagnyou池,访问baidu.com时,跳转到真实的百度(无法测试),默认的IP或域名访问时,默认抛给后面的tarena池。
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端也能识别)
[root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
frontend zdyname
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
bind 172.10.10.5:80
acl bd_zdyname hdr(host) -i baidu.com
acl cy_zdyname hdr(host) -i www.changyou.com
redirect prefix http://www.baidu.com code 301 if bd_zdyname
use_backend changyou if cy_zdyname
default_backend tarena
backend changyou
balance roundrobin
option httpclose
option forwardfor
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
backend tarena
balance roundrobin
option httpclose
option forwardfor
server web2 192.168.4.200:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# echo changyou > /changyou/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
两台web服务器上配置:(192.168.4.200上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo tarena > /tarena/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
客户端访问调度器测试:(172.10.10.6)
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com www.test.com
[root@localhost 桌面]# curl baidu.com //无环境,无法测试。
curl: (6) Couldn't resolve host 'baidu.com'
[root@localhost 桌面]# curl www.changyou.com //当用changyou域名访问时,仅仅解析到changyou池
changyou
[root@localhost 桌面]# curl www.tarena.com
tarena
[root@localhost 桌面]# curl www.test.com //当默认访问时,仅仅解析到tarena池
tarena
[root@localhost 桌面]# curl 172.10.10.5 //当默认访问时,仅仅解析到tarena池
tarena
说明:客户端也可输入:http;//调度器IP或域名/admin?stats来查看后面各个节点是否处于正常状态,用户名和密码为:admin,123456,访问后如下图:
项目2:基于访问站点下的不同目录时,到后面对应池中找服务器的对应站点目录
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端也能识别)
[root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
frontend zdyname
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
bind 172.10.10.5:80
acl zdy_cy path_beg /changyou
acl zdy_tarena path_beg /tarena
use_backend changyou if zdy_cy
use_backend tarena if zdy_tarena
backend changyou
balance roundrobin
option httpclose
option forwardfor
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
backend tarena
balance roundrobin
option httpclose
option forwardfor
server web2 192.168.4.200:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /changyou/changyou -p
[root@localhost 桌面]# echo changyou > /changyou/changyou/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
两台web服务器上配置:(192.168.4.200上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /tarena/tarena -p
[root@localhost 桌面]# echo tarena > /tarena/tarena/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
客户端访问调度器测试:(172.10.10.6)
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com www.test.com
[root@localhost 桌面]# curl www.changyou.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@localhost 桌面]# curl www.tarena.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@localhost 桌面]# curl www.changyou.com/changyou/
changyou
[root@localhost 桌面]# curl www.tarena.com/tarena/
tarena
两台web把各自站点目录都删除:
192.168.4.100上:
[root@localhost 桌面]# rm -rf /changyou/changyou/
192.168.4.200上:
[root@localhost 桌面]# rm -rf /tarena/tarena/
客户端(172.10.10.6)访问调度器,客户端访问如下结果:
[root@localhost 桌面]# curl www.changyou.com/changyou/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /changyou/ was not found on this server.</p>
<hr>
<address>Apache/2.2.15 (Red Hat) Server at www.changyou.com Port 80</address>
</body></html>
[root@localhost 桌面]# curl www.tarena.com/tarena/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /tarena/ was not found on this server.</p>
<hr>
<address>Apache/2.2.15 (Red Hat) Server at www.tarena.com Port 80</address>
</body></html>
说明:客户端也可输入:http;//调度器IP或域名/admin?stats来查看后面各个节点是否处于正常状态,用户名和密码为:admin,123456,访问如下图:
项目3:基于访问站点目录下的扩展名访问资源时,抛给对应后端池服务器访问
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端也能识别)
[root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
frontend zdyname
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
bind 172.10.10.5:80
acl zdy_pic path_end .gif .png .jpg .css .js
acl zdy_static path_end .gif .png .jpg .css
use_backend static_changyou if zdy_pic or zdy_static
backend static_changyou
balance roundrobin
option httpclose
option forwardfor
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
backend tarena
balance roundrobin
option httpclose
option forwardfor
server web2 192.168.4.200:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# mv changyou.jpg /changyou/
[root@localhost 桌面]# ls /changyou/
changyou.jpg
[root@localhost 桌面]# /etc/init.d/httpd restart
两台web服务器上配置:(192.168.4.200上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# mv tarena.jpg /tarena/
[root@localhost 桌面]# ls /tarena/
tarena.jpg
[root@localhost 桌面]# /etc/init.d/httpd restart
客户端访问调度器测试:(172.10.10.6)
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com www.test.com
说明:客户端也可输入:http;//调度器IP或域名/admin?stats来查看后面各个节点是否处于正常状态,用户名和密码为:admin,123456,访问时如下图:
项目4:定义域名acl规则,根据不同域名抛给后面对应服务器池中,但基于端口和IP访问限制做控制
要求:访问www.chagnyou.com时,抛给后面的chagnyou池,默认的IP或域名访问时,默认抛给后面的tarena池。
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端也能识别)
[root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
frontend zdyname
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
bind 172.10.10.5:80
#acl zdy_valid_ip src 192.168.4.0/24 //也可对网段进行限制,这里是合法的网段
acl zdy_valid_ip src 172.10.10.6 #//这里是合法的IP
block if !zdy_valid_ip #//如果不是合法的IP,则丢弃,不给代理
acl cy_zdyname hdr(host) -i www.changyou.com
use_backend changyou if cy_zdyname
default_backend tarena
backend changyou
balance roundrobin
option httpclose
option forwardfor
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
backend tarena
balance roundrobin
option httpclose
option forwardfor
server web2 192.168.4.200:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# echo changyou > /changyou/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
两台web服务器上配置:(192.168.4.200上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo tarena > /tarena/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
客户端访问调度器测试:(172.10.10.6) //成功给代理
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com www.test.com
[root@localhost 桌面]# curl www.changyou.com //当用changyou域名访问时,仅仅解析到changyou池
changyou
[root@localhost 桌面]# curl www.tarena.com
tarena
[root@localhost 桌面]# curl www.test.com //当默认访问时,仅仅解析到tarena池
tarena
[root@localhost 桌面]# curl 172.10.10.5 //当默认访问时,仅仅解析到tarena池
tarena
客户端访问调度器测试:(172.10.10.8) //不给代理,访问不到
[root@localhost 桌面]# curl www.changyou.com
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
[root@localhost 桌面]# curl www.tarena.com
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
[root@localhost 桌面]# curl www.test.com
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
[root@localhost 桌面]# curl 172.10.10.5
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
说明:客户端也可输入:http;//调度器IP或域名/admin?stats来查看后面各个节点是否处于正常状态,用户名和密码为:admin,123456,访问后如下图:
相关推荐
- HTTP 和 HTTPS 有何不同?一文带你全面了解
-
随着互联网时代的高速发展,Web服务器和客户端之间的安全通信需求也越来越高。HTTP和HTTPS是两种广泛使用的Web通信协议。本文将介绍HTTP和HTTPS的区别,并探讨为什么HTTPS已成为We...
- HTTP和HTTPS的区别?
-
本文主要讲解http和https的关系与区别,分辨不清区别的同学要注意朝下看完,Web面试中最常问的已到面试题~~一.HTTP和HTTPS的相同点:大多数情况下,HTTP和HTTPS是相同的,...
- 详解HTTP协议与RESTFUL
-
1.HTTP简介http协议是一种超文本传输协议,主要应用在浏览器与服务器之间的通信,可以传输文本,图片,视频等。它是一种应用层协议,也是基于TCP协议,当然现在流行的Https协议是在TLS或SSL...
- http与https的区别,读完之后,大部分程序员收藏了...
-
在URL前加https://前缀表明是用SSL加密的。你的电脑与服务器之间收发的信息传输将更加安全。Web服务器启用SSL需要获得一个服务器证书并将该证书与要使用SSL的服务器绑定。http和ht...
- JMeter测试HTTP GET请求(附实例)
-
一、HTTPRequest配置项解析●WebServer:1.Protocol[http]:○若为HTTP协议可以不填写(默认为HTTP);○若为HTTPS协议可以填写“https”;还可...
- 2019山东高考分数线公布:本科文503 理443
-
刚刚,2019年山东高考各批次录取最低分数线公布了!6月24日下午,山东省教育厅举行2019年山东高考第二场新闻发布会。山东省教育招生考试院在发布会上公布了山东今年高招各批次录取控制分数线。其中,本科...
- Linux系统网站出现503错误提示怎么解决?
-
当Linux系统上的网站出现503ServiceUnavailable错误时,通常表示服务器暂时无法处理请求,可能由后端服务崩溃、资源耗尽或配置错误导致。以下是系统化的排查和解决方案:一、...
- 三石说:一文带你了解Https
-
今天我们继续深入http,本篇将介绍Https的内容,相信你看过之后对https有一定的了解。HTTPSHTTPS(全称:HyperTextTransferProtocoloverSecu...
- HTTP与HTTPS的区别
-
首先,需要知道HTTP和HTTPS是什么。HTTP是超文本传输协议,是一个基于请求与响应,无状态的,应用层的协议,常基于TCP/IP协议传输数据,是互联网上应用最为广泛的一种网络协议。也...
- Caddy服务器开启HTTP/3:如何让你的网站快如闪电?
-
Caddy服务器开启HTTP/3:如何让你的网站快如闪电?在互联网技术飞速迭代的今天,HTTP/3正以革命性的姿态颠覆传统网络传输模式。作为首个基于QUIC协议的HTTP标准,它不仅能大幅提升网站加载...
- HTTP/1.1、HTTP/2、HTTP/3 演变
-
HTTP基本概念HTTP是超文本传输协议,也就是HyperTextTransferProtocol。HTTP常见的状态码有哪些?1xx类状态码属于提示信息,是协议处理中的一种中间状态,实际...
- HTTP/3 黑科技:三次握手如何进阶 QUIC?30 年通信细节揭秘
-
大家好,我是“极客运维社”的飞哥,点击右上方“关注”,每天和大家分享关于网络设备及系统和企业组网方面干货。码字不易,如果您觉得文章还可以,就点赞+关注+收藏吧,也许在以后某个时间能够用得到。H...
- 总结HTTP/HTTPS协议基础的有那些漏洞,怎么检查,怎么防范
-
以下是基于黑盒测试、白盒测试和灰盒测试视角对HTTP/HTTPS协议漏洞检查与防范的分类整理:一、黑盒测试(外部视角,无内部权限)定义:模拟攻击者视角,仅通过外部网络接口进行测试,不依赖系...
- 什么是HTTP? HTTP 和 HTTPS 的区别?
-
HTTP(HyperTextTransferProtocol),即超文本运输协议,是实现网络通信的一种规范。HTTP是一个传输协议,即将数据由A传到B或将B传输到A,并且A与B之间能够存...
- 一篇文章搞懂HTTP和HTTPS的的本质区别
-
http协议是基于tcp协议,默认是80端口。它的特点是什么?它是基于请求和响应的,大家抓个包能看到http协议有一个请求报文有一个响应报文,还有它是一个无状态的协议,还有一个无连接的协议。无连接是指...
你 发表评论:
欢迎- 一周热门
-
-
爱折腾的特斯拉车主必看!手把手教你TESLAMATE的备份和恢复
-
如何在安装前及安装后修改黑群晖的Mac地址和Sn系列号
-
[常用工具] OpenCV_contrib库在windows下编译使用指南
-
WindowsServer2022|配置NTP服务器的命令
-
Ubuntu系统Daphne + Nginx + supervisor部署Django项目
-
WIN11 安装配置 linux 子系统 Ubuntu 图形界面 桌面系统
-
解决Linux终端中“-bash: nano: command not found”问题
-
NBA 2K25虚拟内存不足/爆内存/内存占用100% 一文速解
-
Linux 中的文件描述符是什么?(linux 打开文件表 文件描述符)
-
K3s禁用Service Load Balancer,解决获取浏览器IP不正确问题
-
- 最近发表
- 标签列表
-
- linux 查询端口号 (58)
- docker映射容器目录到宿主机 (66)
- 杀端口 (60)
- yum更换阿里源 (62)
- internet explorer 增强的安全配置已启用 (65)
- linux自动挂载 (56)
- 禁用selinux (55)
- sysv-rc-conf (69)
- ubuntu防火墙状态查看 (64)
- windows server 2022激活密钥 (56)
- 无法与服务器建立安全连接是什么意思 (74)
- 443/80端口被占用怎么解决 (56)
- ping无法访问目标主机怎么解决 (58)
- fdatasync (59)
- 405 not allowed (56)
- 免备案虚拟主机zxhost (55)
- linux根据pid查看进程 (60)
- dhcp工具 (62)
- mysql 1045 (57)
- 宝塔远程工具 (56)
- ssh服务器拒绝了密码 请再试一次 (56)
- ubuntu卸载docker (56)
- linux查看nginx状态 (63)
- tomcat 乱码 (76)
- 2008r2激活序列号 (65)