ree12 发表于 2014-9-22 09:54:41

Haproxy实现动静分离和Discuz论坛部署

1.实验拓扑:

2.后端部署Httpd服务器和Mysql服务器    1.安装程序
      分别在lab2,lab3,lab4上安装httpd,
      在lab3,lab4上安装php以及php-mysql
    2.部署论坛
      #在Mysql服务器上创建一个Discuz数据库

1
mysql> create database discuz;




      #为Mysql提供远程访问帐号,并配置密码

1
mysql> grant all on discuz.* to bbs@'172.16.21.%' identified by '123456';




      #将论坛程序解压到lab3的网站根目录,通过网页访问lab3,安装论坛程序

论坛程序已经可以访问

将网站根目录复制到lab4及lab2的对应目录下,并保证文件权限

1
2
# scp -r lab3:/var/www/html/* /var/www/html/
# scp -r lab2:/var/www/html/* /var/www/html/





    3.创建配置共享存储

1
2
3
4
5
6
# cat /etc/exports
/var/www/html/data 172.16.21.0/24(rw,all_squash,anonuid=48,insecure)
#lab3挂载
# mount lab2:/var/www/html/data/ /var/www/html/data/
#lab4挂载
# mount lab2:/var/www/html/data/ /var/www/html/data/





3.前端Haproxy配置#Haproxy配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# cat /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global #全局配置
    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 web *:80 #前端接收设置
acl url_static path_beg-i/data /static /images /javascript /stylesheets #定义静态内容前缀
acl url_static path_end-i.jpg .gif .png .css .js .html .ico #定义静态内容后缀
use_backendstatic if url_static
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static #后端静态服务器设置
    balance   roundrobin
    server      static 172.16.21.102:80 check
    option forwardfor except 127.0.0.1/8
    option originalto
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app #后端动态服务器设置
    balance   roundrobin #使用轮询的调度方式
    cookie srv insert nocache #插入cookie,使其可以访问到后端的同一台动态服务器,防止登录信息丢失
    serverapp1 172.16.21.103:80 check cookie app1
    serverapp2 172.16.21.104:80 check cookie app2
    option forwardfor except 127.0.0.1/8 #添加X-forwarded-for头部信息,向后端主机提供真实访问IP,需要在httpd配置文件中修改
    option originalto
listen admin_stat #状态页面设置
    bind *:8080
    stats enable
    stats uri /haproxy
    stats refresh 10s
    stats auth haadmin:haadmin
    stats admin if TRUE
    option forwardfor except 127.0.0.1/8
option originalto





启动Haproxy,访问论坛
发帖,图片上传测试

#帖子图片访问正常

#清除Cookie后访问成功

#Httpd配置调整
将日志格式中需要记录访问IP的%h更改为{X-Forwarded-For}i,以方便记录统计数据

页: [1]
查看完整版本: Haproxy实现动静分离和Discuz论坛部署