haproxy配置详解以及动静分离的实现

一、前面已经介绍过关于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 :80LISTEN0128:::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.117static.luojianlong.com[root@dynamic ~]# service httpd start[root@dynamic ~]# ss -anplt | grep :80LISTEN0128:::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<?phpecho “dynamic.luojianlong.com”?>[root@dynamic ~]# curl -I HTTP/1.1 200 OKDate: Thu, 03 Apr 2014 05:39:23 GMTServer: Apache/2.2.15 (CentOS)X-Powered-By: PHP/5.3.3Connection: closeContent-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.cfggloballog127.0.0.1 local2chroot/var/lib/haproxypidfile/var/run/haproxy.pidmaxconn4000userhaproxygrouphaproxydaemonstats socket /var/lib/haproxy/statsdefaultsmodehttplogglobaloptionhttplogoptiondontlognulloption http-server-closeoption forwardforexcept 127.0.0.0/8optionredispatchretries3timeout http-request 10stimeout queue1mtimeout connect10stimeout client1mtimeout server1mtimeout http-keep-alive 10stimeout check10smaxconn3000listen statsmode httpbind 0.0.0.0:1080stats enablestats hide-versionstats uri/haproxyadmin?statsstats realm Haproxy\ Statisticsstats auth admin:adminstats admin if TRUEfrontend http-inbind *:80mode httplog globaloption httpcloseoption logasapoption dontlognullcapture request header Host len 20capture request header Referer len 60acl url_staticpath_beg-i /static /images /javascript /stylesheetsacl url_staticpath_end-i .html .jpg .jpeg .gif .png .css .jsacl url_dynamicpath_end-i .php .jspuse_backend static_serversif url_staticuse_backend dynamic_serversif url_dynamicbackend static_serversbalance roundrobinserver imgsrv1 192.168.30.117:80 check maxconn 6000# server imgsrv2 192.168.30.118:80 check maxconn 6000backend dynamic_serversbalance sourceserver websrv1 192.168.30.119:80 check maxconn 1000# server websrv2 192.168.30.120:80 check maxconn 1000

接下来,启动haproxy

[root@haproxy ~]# service haproxy startStarting haproxy:[ OK ][root@haproxy ~]# ss -anptl | grep :80LISTEN0128*:80*:*users:((“haproxy”,20436,6))

正常启动,打开浏览器访问管理界面::1080/haproxyadmin?stats

发现定义的static_servers和dynamic_servers已经正常工作

haproxy的管理界面支持直接禁用backend_server,使客户端无法访问:

接下来验证下动静分离的效果,打开浏览器访问

发现访问的192.168.30.117,为静态内容服务器

访问

发现访问的192.168.30.119,为动态内容服务器

到此已经实现了动静分离的效果。

本文出自 “龍行天下” 博客,,请务必保留此出处

为你的难过而快乐的是敌人,

haproxy配置详解以及动静分离的实现

相关文章:

你感兴趣的文章:

标签云: