-----client---------haproxy-------nginx1---------nginx2------
| | | |
192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20
一、安装haproxy
1、安装
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar -zxvf haproxy-1.5.19.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/haproxy-1.5.19/
[root@localhost ~]# make TARGET=linux26 PREFIX=/usr/local/haproxy
注意:linux26是指linux的内核版本号。
[root@localhost ~]# make install PREFIX=/usr/local/haproxy
2、配置haproxy
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost ~]# cp /usr/src/haproxy-1.5.19/examples/haproxy.cfg /etc/haproxy/
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
修改:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/share/haproxy
uid 99
gid 99
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
# redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen webcluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server inst1 192.168.1.10:80 check inter 2000 fall 3
server inst2 192.168.1.20:80 check inter 2000 fall 3
注意:
如果启动时出现报错:/haproxy.main()] Cannot chroot(/usr/share/haproxy)
则手动创建:
[root@localhost ~]# mkdir /usr/share/haproxy
如果启动时出现报错:Starting proxy cacti: cannot bind socket
则执行:
[root@localhost ~]# sysctl -e net.ipv4.ip_nonlocal_bind=1
3、启动haproxy
[root@localhost ~]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/ //注意软链接的目录
[root@localhost ~]# cp /usr/src/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# /etc/init.d/haproxy start
[root@localhost ~]# /etc/init.d/haproxy status
[root@localhost ~]# netstat -anp | grep haproxy //占用的也是TCP的80端口
[root@localhost ~]# chkconfig --add haproxy
[root@localhost ~]# chkconfig haproxy on
二、安装Nginx
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl openssl-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar -zxvf nginx-1.6.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.6.0/
[root@localhost nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
[root@localhost nginx-1.6.0]# make && make install
[root@localhost ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost ~]# nginx -t
[root@localhost ~]# nginx
[root@localhost ~]# netstat -anpt | grep 80
[root@localhost ~]# killall -s HUP nginx //重新加载
[root@localhost ~]# killall -s QUIT nginx //关闭服务
[root@localhost ~]# nginx
创建页面文件:
web-1:
[root@localhost ~]#echo "welcome to 192.168.1.20 web server" > /usr/local/nginx/html/index.html
web-2:
[root@localhost ~]#echo "welcome to 192.168.1.30 web server" > /usr/local/nginx/html/index.html
[root@localhost ~]# firefox http://localhost/ &
三、验证:
客户端输入:
http://192.168.1.1/index.html
http://192.168.1.1/index.html
断开其中一个节点,再访问:
http://192.168.1.1/index.html
http://192.168.1.1/index.html
四、设置haproxy日志
[root@localhost ~]# mkdir /var/log/haproxy
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
修改:
注释(两行):
#log 127.0.0.1 local0
#log 127.0.0.1 local1 notice
添加(两行):
log /dev/log local0 info //启动访问信息
log /dev/log local0 notice //启动通告,通知等信息
[root@localhost ~]# /etc/init.d/haproxy stop
[root@localhost ~]# /etc/init.d/haproxy start
[root@localhost ~]# vim /etc/rsyslog.d/haproxy.conf
添加:
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~
五、验证:
[root@localhost ~]# /etc/init.d/rsyslog restart
客户端输入:
http://192.168.1.1/index.html
http://192.168.1.1/index.html
查看:
[root@localhost ~]# tail -f /var/log/haproxy/haproxy-info.log
日志会记录客户端访问信息
[root@localhost ~]# tail -f /var/log/haproxy/haproxy-notice.log
日志会记录haproxy启动/停止信息