莫问 发表于 2019-1-2 10:07:35

基于HAproxy的web动静分离及输出状态检测页面

  一、简介
      HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代 理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
   HAProxy是免费、极速且可靠的用于为TCP和基于HTTP应用程序提供高可用、负载均衡和代理服务的解决方案,尤其适用于高负载且需要持久连接或7层处理机制的web站点。
二、安装配置
1.拓扑图

http://s3.运维网.com/wyfs02/M01/25/8F/wKiom1NkiSjh5emhAAENceqyIUY124.jpg
  说明:1.由于测试环境虚拟机有限,此处将静态页面和图片放在了一台机器上
  2.由于测试环境虚拟机有限,此处动态页面和静态页面各提供一台机器,不考虑单点故障
  3.用户看到的服务器为HAproxy对外提供的ip地址,看不到后面的各服务器组

  4.此处不涉及php页面与数据库的交互内容

  2.ip地址规划

功用ip地址安装软件操作系统反向代理服务器192.168.1.201Haproxycentos6.5静态页面服务器192.168.1.202httpdcentos6.5动态页面服务器
192.168.1.203httpd、phpcentos6.5  3.安装配置HAproxy

  ①、安装

  HAproxy可以通过yum源安装也可以通过源码编译安装,经查看得知yum源中提供的1.4.24版本,而官方提供的为1.4.25版本,所有本次我们通过yum来进行安装
# yum installhaproxy -y  ②、启动配置文件选项

# vi /etc/rsyslog.conf
启用一下两行
$ModLoad imudp
$UDPServerRun 514
添加以下一行
local2.*                     /var/log/haproxy.log  将rsyslog日志文件系统重启

  ③、配置动静分离

#---------------------------------------------------------------------
# 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
#---------------------------------------------------------------------
frontendmain *: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_statichdr_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 192.168.1.202:80 check maxconn 2000
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance   roundrobin
serverapp1 192.168.1.203:80    check maxconn 200  ④、准备静态页面与动态页面
静态页面
# vi /var/www/html/index.html
hello,wangfeng7399  动态页

# yum install -y php httpd
# vi index.php

~  ⑤、测试

http://s3.运维网.com/wyfs02/M01/25/90/wKioL1NkmUmjxYkoAATbpVIjZps749.jpg
http://s3.运维网.com/wyfs02/M00/25/90/wKiom1NkmXTB5CN3AABw1VvkVmM647.jpg
  ⑥、启动状态输出页面,在配置文件添加如下行

listen stats
mode http
bind *:1090
stats enable
stats hide-version
stats uri   /hadmin?stats
stats realm   Haproxy\ Statistics
stats auth    admin:admin
stats admin if TRUE  重新载入服务

http://s3.运维网.com/wyfs02/M01/25/90/wKiom1NkmumTDo-qAACjYZ2UDWw454.jpg
http://s3.运维网.com/wyfs02/M00/25/90/wKioL1Nkmr_S1e-nAAhWLhOGAe8217.jpg
  大功告成 ,由于本人水平有限,可能有错误,请各位大牛匹配指正

  




页: [1]
查看完整版本: 基于HAproxy的web动静分离及输出状态检测页面