<pre name="code" class="sql"><pre name="code" class="sql">yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses* libmcrypt*
# tar zcvf haproxy-1.3.20.tar.gz
# cd haproxy-1.3.20
# make TARGET=linux26 PREFIX=/usr/local/haproxy #将haproxy安装到/usr/local/haproxy
# make install PREFIX=/usr/local/haproxy
jrhppt01:/root# ps -ef | grep haproxy
root 11889 11871 0 10:33 pts/0 00:00:00 grep haproxy
haproxy 31872 1 0 Apr16 ? 02:09:08 /usr/sbin/haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
启动程序:
haproxy:/usr/local/haproxy/sbin# ls
haproxy haproxy-systemd-wrapper
配置文件路径:
/etc/haproxy
配置内容:
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local3
maxconn 65535
chroot /usr/local/haproxy
uid 401
gid 401
daemon
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
option abortonclose
option httpclose
option forwardfor
maxconn 65535
timeout connect 5000
timeout client 50000
timeout server 50000
timeout check 5s
stats refresh 30s
stats uri /stats
stats realm baison-test-Haproxy
stats auth admin:zjadmin818
stats hide-version
frontend www
bind *:80
bind *:443 ssl crt /etc/haproxy/cert.pem
redirect scheme https if !{ ssl_fc }
acl host_zjcap.cn hdr_beg(host) -i zjcap.cn
redirect prefix http://www.zjcap.cn if host_zjcap.cn
####flow
acl web_req path_beg -i /web
use_backend appserver_8081 if web_req
####flow
acl business_req path_beg -i /business
use_backend appserver_8081 if business_req
acl validcode_req path_beg -i /validcode
use_backend appserver_8081 if validcode_req
####api
acl api_req path_beg -i /api
use_backend appserver_8082 if api_req
###web nginx
acl mgr_req path_beg -i /backoffice
use_backend mgrserver_8001 if mgr_req
###pay
acl pay_req path_beg -i /pay
use_backend appserver_8083 if pay_req
acl llmock_req path_beg -i /llmock
use_backend appserver_8083 if llmock_req
###web
acl url_static path_end .html .gif .png .jpg .css .js
use_backend webserver_8001 if url_static
default_backend webserver_8001
###frond end
backend webserver_8001
mode http
balance roundrobin
server webhost01_8001 10.252.105.157:8001 check inter 2000 fall 3
server webhost02_8001 10.168.29.17:8001 check inter 2000 fall 3
####backoffice
backend mgrserver_8001
mode http
server mgrhost_8001 10.168.34.94:8001 check inter 2000 fall 3
######flow
backend appserver_8081
mode http
balance roundrobin
server apphost01_8081 10.168.32.82:8081 check inter 2000 fall 3
server apphost02_8081 10.168.33.193:8081 check inter 2000 fall 3
######api
backend appserver_8082
mode http
balance roundrobin
server apphost11_8082 10.171.243.55:8082 check inter 2000 fall 3
server apphost12_8082 10.252.148.164:8082 check inter 2000 fall 3
#######pay
backend appserver_8083
mode http
balance roundrobin
server apphost21_8083 10.171.240.139:8083 check inter 2000 fall 3
server apphost22_8083 10.252.163.244:8083 check inter 2000 fall 3
listen stats 0.0.0.0:8899
mode http
option httpclose
balance roundrobin
stats uri /admin-status
stats realm Haproxy Statistics
stats auth admin:admin-check
简单测试:
acl api_req path_beg -i /api
use_backend appserver_8082 if api_req
acl url_static path_end .html .gif .png .jpg .css .js
use_backend webserver_8001 if url_static
default_backend webserver_8001
backend appserver_8082
mode http
balance roundrobin
server apphost11_8082 10.171.243.55:8082 check inter 2000 fall 3
server apphost12_8082 10.252.148.164:8082 check inter 2000 fall 3
backend webserver_8001
mode http
balance roundrobin
server webhost01_8001 10.252.105.157:8001 check inter 2000 fall 3
server webhost02_8001 10.168.29.17:8001 check inter 2000 fall 3
haproxy:
192.168.32.173 http://192.168.32.173/api --可以访问
http://192.168.32.173/ --跳到静态页