一 Haproxy簡介
Haproxy提供高可用性,負載均衡以及基於TCP和HTTP應用代理,支持虛擬機,它是免費的,快速並且可靠的一種解決方案。Haproxy特別適用負載大的web站點,這些站點通常有需要會話保持或七層處理。Haproxy運行在當前的硬件上,完全可以支持數以萬計的併發連接。並且它的運行模式使得它可以很簡單安全的整合進你當前的架構中,同時保護你的WEB服務器不被暴露到網上。
實驗
iP 規劃
Haproxy 172.16.31.30
靜態服務器 172.16.31.31
動態服務器 172.16.31.32
1 安裝Haproxy並且啓動配置文件選項
# yum install haproxy -y
#vi /etc/rsyslog.conf
啓用一下兩行
$ModLoad imudp
$UDPServerRun 514
添加以下一行
local2.* /var/log/haproxy.log
2 配置動靜分離
#———————————————————————
# 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 *:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js .html
acl host_static hdr_beg(host) -i img. video. download. ftp. imgs.
use_backend static if url_static
default_backend app
#———————————————————————
# static backend for serving up images, stylesheets and such
#———————————————————————
backend static
balance roundrobin
server static 172.16.31.31:80 check maxconn 2000
#———————————————————————
# round robin balancing between the various backends
#———————————————————————
backend app
balance roundrobin
server app1 172.16.31.32:80 check maxconn 200
3準備靜態和動態頁面
在31主機上準備靜態頁面
# vi /var/www/html/index.html
hello
在32主機上準備動態頁面
# vi index.php
<?php
phpinfo();
?>