本節我們來講一講haproxy的相關知識。
HAProxy提供高可用性、負載均衡以及基於TCP和HTTP應用的代 理,支持虛擬主機,它是免費、快速並且可靠的一種解決方案。HAProxy特別適用於那些負載特大的web站點,這些站點通常又需要會話保持或七層處理。HAProxy運行在當前的硬件上,完全可以支持數以萬計的併發連接。並且它的運行模式使得它可以很簡單安全的整合進您當前的架構中, 同時可以保護你的web服務器不被暴露到網絡上。
好的下面我們就來實現一下這個過程。
實驗拓撲圖:
代理服務器爲內部的2臺WEB服務進行代理,其中一臺是動態內容的服務器,另一臺是靜態內容的服務器,haproxy會自動識別出請求的內容爲動態還是靜態,從而將請求分發到各自的服務器進行響應。
動態服務器
該服務器中安裝了PHP服務,這裏我是直接yum安裝的。因此可以響應PHP請求,在主頁中放的是一個PHP網頁,注意,相應的在httpd服務器的配置文件中需要修改的一些選項還是得修改的。
靜態服務器
該服務器中沒有安裝PHP,只在跟目錄中放了一個普通的主頁面
代理服務器
直接yum安裝haproxy
修改配置文件
[root@www ~]# vim /etc/haproxy/haproxy.cfg
#--------------------------------------------------------------------- # Example configuration for a possible web application. See the # full configuration options online. # # http://haproxy.1wt.eu/download/1.4/doc/configuration.txt # #--------------------------------------------------------------------- #--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy #全局配置端不需要改動 pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- 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 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend main bind :80 #綁定端口 acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets #定義靜態內容前綴 acl url_static path_end -i .jpg .gif .png .css .js .html #定義靜態內容後綴 use_backend static if url_static default_backend app #--------------------------------------------------------------------- # static backend for serving up p_w_picpaths, stylesheets and such #--------------------------------------------------------------------- backend static balance roundrobin server static 172.16.6.1:80 check #定義後端靜態內容服務器 #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend app balance roundrobin server app1 172.16.6.2:80 check #定義後端動態內容服務器
配置完成啓動服務
下面就可以開始測試了
靜態網頁:
動態網頁:
OK,這樣我們的一個最簡單的動靜分離就實現了,謝謝!