转:http://www.cnblogs.com/dkblog/archive/2011/07/06/2098949.html
http://www.cnblogs.com/super-d2/p/4134562.html
http://blog.csdn.net/youyudehexie/article/details/7592206
51 make TARGET=linux26 PREFIX=/usr/local/haproxy install
vim /usr/local/haproxy/conf/haproxy.cfg
global maxconn 51200 chroot /usr/local/haproxy uid 99 gid 99 daemon #quiet nbproc 1 pidfile /usr/local/haproxy/logs/haproxy.pid defaults mode http #retries 2 option redispatch option abortonclose timeout connect 5000ms timeout client 30000ms timeout server 30000ms #timeout check 2000 log 127.0.0.1 local0 err #[err warning info debug] balance roundrobin # option httplog # option httpclose # option dontlognull # option forwardfor listen admin_stats bind 0.0.0.0:8888 option httplog stats refresh 30s stats uri /stats stats realm Haproxy Manager stats auth admin:admin #stats hide-version listen test1 bind :12345 mode tcp server t1 10.77.50.111:8088 server t2 10.77.50.111:8099 listen test2 bind :80 option httpclose option forwardfor server s1 10.77.50.111:8088 check weight 1 minconn 1 maxconn 3 check inter 40000 server s2 10.77.50.68:808 check weight 1 minconn 1 maxconn 3 check inter 40000 //本机nginx运行在808
root@test-VirtualBox:/usr/local/haproxy/conf# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
测试
访问http://10.77.50.68
1.安装
wget http://download.chinaunix.net/download.php?id=25784&ResourceID=12508
如果源有问题,换wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.9.tar.gz
tar -zxvf haproxy-1.3.15.10.tar.gz
cd haproxy-1.3.15.10
make TARGET=linux26 PREFIX=/usr/local/haproxy (#将haproxy安装到/usr/local/haproxy)
make install PREFIX=/usr/local/haproxy
cd /usr/local/haproxy
vi haproxy.cfg
2.配置
安装完毕后,进入安装目录配置文件,默认情况下目录里是没有.cfg配置文件的,可以回到安装文件目录下将examples下的haproxy.cfg拷贝到usr/local/haproxy下。
haproxy.cfg的内容如下:
# cd /usr/local/haproxy
# vi haproxy.cfg
global
maxconn 51200
chroot /usr/local/haproxy
uid 99
gid 99
daemon
#quiet
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid
defaults
mode http
#retries 2
option redispatch
option abortonclose
timeout connect 5000ms
timeout client 30000ms
timeout server 30000ms
#timeout check 2000
log 127.0.0.1 local0 err #[err warning info debug]
balance roundrobin
# option httplog
# option httpclose
# option dontlognull
# option forwardfor
listen admin_stats
bind 0.0.0.0:8888
option httplog
stats refresh 30s
stats uri /stats
stats realm Haproxy Manager
stats auth admin:admin
#stats hide-version
listen test1
bind :12345
mode tcp
server t1 127.0.0.1:8881
server t2 192.168.1.102:8881
listen test2 :80
option httpclose
option forwardfor
server s1 127.0.0.1:8081 check weight 1 minconn 1 maxconn 3 check inter 40000
server s2 127.0.0.1:8082 check weight 1 minconn 1 maxconn 3 check inter 40000
3.启动Haproxy
[root@node3 haproxy]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg [root@node3 app]# ps -ef |grep haproxy |grep -v grep root 6950 1 0 19:35 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
访问:http://192.168.2.150:8888/stats
4.测试:
由于我们把环回接口绑定127.0.0.1:8081端口到本机127.0.0.1:80端口上,
我们上次安装的httpd,绑定的是127.0.0.1:8081
http://www.cnblogs.com/super-d2/p/4134467.html
访问http://192.168.2.137:8081/可看到这个
而没配置haproxy之前,我们访问http://192.168.2.137:80/是访问不到的,因为这个端口80上没有这项服务;
如果现在我们启动haproxy的话,
再次访问:
可以通过通过haproxy我们实现了代理访问
http://192.168.2.137:80/
5.设置为服务命令
vim /etc/rc.d/init.d/haproxy
脚本如下:
#!/bin/bash BASE_DIR="/usr/local/haproxy" ARGV="$@" start() { echo "START HAPoxy SERVERS" $BASE_DIR/sbin/haproxy -f $BASE_DIR/haproxy.cfg } stop() { echo "STOP HAPoxy Listen" kill -TTOU $(cat $BASE_DIR/logs/haproxy.pid) echo "STOP HAPoxy process" kill -USR1 $(cat $BASE_DIR/logs/haproxy.pid) } case $ARGV in start) start ERROR=$? ;; stop) stop ERROR=$? ;; restart) stop start ERROR=$? ;; *) echo "hactl.sh [start|restart|stop]" esac exit $ERROR
chmod +x /etc/rc.d/init.d/haproxy
启动与停止haproxy
[root@node3 ~]# service haproxy stop STOP HAPoxy Listen STOP HAPoxy process [root@node3 ~]# ps -ef |grep haproxy |grep -v grep [root@node3 ~]# service haproxy start START HAPoxy SERVERS [root@node3 ~]# ps -ef |grep haproxy |grep -v grep root 11259 1 0 15:33 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg