一、前面已经介绍过关于haproxy的工作特性了,本文主要介绍下haproxy如何来实现web应用的动静分离的,主要思路就是通过frontend段中定义acl访问控制,把符合静态内容的请求归类到一个acl,符合动态的请求归类到另外一个acl,backend段中定义接收请求的静态内容和动态内容的服务器,然后通过use_backend调用定义的acl即可。
二、实验环境:
192.168.30.116 OS:CentOS6.4 x86_64 haproxy.luojianlong.com
192.168.30.117 OS:Centos6.4 x86_64 static.luojianlong.com
192.168.30.119 OS:Centos6.4 x86_64 dynamic.luojianlong.com
haproxy version:haproxy-1.4.24
拓扑图如下:
在haproxy server上安装haproxy,这里使用yum安装
[root@haproxy ~]# yum -y install haproxy
在static server,dynamic上安装httpd和php
[root@static ~]# yum -y install httpd
[root@dynamic ~]# yum -y install httpd php
分别启动static,dynamic的httpd,测试本地访问是否正常
[root@static ~]# service httpd start
[root@static ~]# ss -naptl | grep :80
LISTEN 0 128 :::80 :::* users:(("httpd",24245,5),("httpd",24247,5),("httpd",24248,5),("httpd",24249,5),("httpd",24250,5),("httpd",24251,5),("httpd",24252,5),("httpd",24253,5),("httpd",24254,5))
[root@static ~]# echo "static.luojianlong.com" >> /var/www/html/index.html
[root@static ~]# curl 192.168.30.117
static.luojianlong.com
[root@dynamic ~]# service httpd start
[root@dynamic ~]# ss -anplt | grep :80
LISTEN 0 128 :::80 :::* users:(("httpd",13702,5),("httpd",13704,5),("httpd",13705,5),("httpd",13706,5),("httpd",13707,5),("httpd",13708,5),("httpd",13709,5),("httpd",13710,5),("httpd",13711,5))
[root@dynamic ~]# vi /var/www/html/index.php
<?php
echo "dynamic.luojianlong.com"
?>
[root@dynamic ~]# curl -I
HTTP/1.1 200 OK
Date: Thu, 03 Apr 2014 05:39:23 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Connection: close
Content-Type: text/html; charset=UTF-8
访问都正常
下面开始配置haproxy
[root@haproxy ~]# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
[root@haproxy ~]# vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
frontend http-in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .html .jpg .jpeg .gif .png .css .js
acl url_dynamic path_end -i .php .jsp
use_backend static_servers if url_static
use_backend dynamic_servers if url_dynamic
backend static_servers
balance roundrobin
server imgsrv1 192.168.30.117:80 check maxconn 6000
# server imgsrv2 192.168.30.118:80 check maxconn 6000
backend dynamic_servers
balance source
server websrv1 192.168.30.119:80 check maxconn 1000
# server websrv2 192.168.30.120:80 check maxconn 1000
stats enable:启用基于程序编译时默认设置的统计报告,不能用于“frontend”区段;