首页 > 代码库 > haproxy动静分离

haproxy动静分离

一 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


配置动静分离

#———————————————————————

# 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();

?>

wKioL1QekBuwlGuPAAEzC3-Y8Uw723.png

本文出自 “Linux” 博客,转载请与作者联系!

haproxy动静分离