haproxy+varnish實現discuz論壇的動靜分離以及靜態緩存


一、目的:

        (1) 動靜分離discuzx,靜態資源通過NFS掛載至動態服務器中; 

        (2) varnish緩存靜態資源;


二、拓撲規劃

wKioL1gm-NqgHsc3AADVZY--o54296.jpg

三、步驟

    1、先部署discuz的動靜分離


        (1)在app動態服務器快速搭建LAMP環境:

yum install -y httpd mysql-server php php-mysql

        (2)創建discuz論壇的mysql用戶:

MySQL [(none)]> GRANT ALL ON *.* TO 'nihao'@'localhost' IDENTIFIED BY '123456';
MySQL [(none)]> FLUSH PRIVILEGES;


        (3)部署discuz論壇程序:

[root@localhost html]# unzip Discuz_X3.2_SC_UTF8.zip


        (4)要實現discuz的動靜分離,首先要了解discuz的動靜資源的存放目錄位置,在discuz的根目錄upload目錄中,data和static存放的是靜態資源:

[root@localhost upload]# tree -L 1
.
├── admin.php
├── api
├── api.php
├── archiver
├── config
├── connect.php
├── cp.php
├── crossdomain.xml
├── data  #靜態資源
├── favicon.ico
├── forum.php
├── group.php
├── home.php
├── index.php
├── install
├── member.php
├── misc.php
├── plugin.php
├── portal.php
├── robots.txt
├── search.php
├── source
├── static  #靜態資源
├── template
├── uc_client
├── uc_server
└── userapp.php
10 directories, 17 files


        (5)知道了靜態資源的位置,接下來就可以搭建static靜態資源的靜態服務器了,首先,快速搭建LAMP環境,這裏就不演示了,搭建完成後啓動NFS和配置NFS:

[root@php ~]# yum install -y rpcbind nfs-utils
[root@php ~]# vim /etc/exports  #編輯NFS共享目錄
[root@php ~]# cat !$
cat /etc/exports
/var/www/html/upload/data 10.1.0.0/16(rw)
/var/www/html/upload/static 10.1.0.0/16(rw)
[root@php ~]# service rpcbind start
[root@php ~]# service ntf start
[root@php ~]# showmount -e 10.1.249.30 
Export list for 10.1.249.30:
/var/www/html/upload/static 10.1.0.0/16
/var/www/html/upload/data   10.1.0.0/16


        (6)回過頭來,先把動態服務的初始化靜態內容複製到靜態服務器上,再動態服務器上將靜態資源的兩個目錄掛上去:

[root@localhost ~]# scp /var/www/html/upload/static/* 10.1.249.30:/var/www/html/upload/static/*  #拷貝文件時路徑要一致
[root@localhost ~]# scp /var/www/html/upload/data/* 10.1.249.30:/var/www/html/upload/data/*
[root@localhost ~]# mount.nfs 10.1.249.30:/var/www/html/upload/static/ /var/www/html/upload/static/ #掛載NFS
[root@localhost ~]# mount.nfs 10.1.249.30:/var/www/html/upload/data/ /var/www/html/upload/data/



        (7)OK,接下來可以安裝haproxy來進行設置acl調度了:

[root@localhost ~]# yum install -y haproxy
[root@localhost ~]# cat /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
#---------------------------------------------------------------------
listen stats
    mode http
    bind *:1080
    stats enable
    stats hide-version
    stats uri     /admin
    stats realm   Haproxy\ Statistics
    stats auth    admin:admin
    stats admin if TRUE
frontend  main 
    bind *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js .html .ico
    use_backend static          if url_static
    default_backend             app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 10.1.249.30:80 check
    rspadd X-Via:static #啓用響應報文首部標誌,以便觀察是靜態服務器反饋的
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server app 10.1.252.36:80 check
    rspadd X-Via:app #啓用響應報文首部標誌,以便觀察是動態服務器反饋的



        (8)OK,haproxy已經搭建完成,我們來測試一下:

haproxy狀態頁:

wKioL1gm-TaQTv20AAP6clWt8-M191.jpg

動態服務器響應:

wKiom1gm-TfBuK9bAAK0mumdaSQ184.jpg

靜態服務器響應:

wKioL1gm-TiTx6dOAAK0GCAMhLk807.jpg


    2、動靜分離已經實現,接下來在靜態資源添加varnish來緩存靜態資源:


        (9)安裝配置varnish,由於前端haproxy已經進行了資源的分離,所以,varnish的配置基本用默認的配置就可以了

[root@localhost ~]# yum install -y varnish-3.0.7-1.el7.centos.x86_64.rpm 
[root@localhost ~]# vim /etc/varnish/default.vcl 
backend default {
  .host = "10.1.249.30";
  .port = "80";
}


        (10)修改HAproxy的配置文件,把靜態資源的調度IP改爲varnish:

[root@localhost ~]# cat  /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
#---------------------------------------------------------------------
listen stats
    mode http
    bind *:1080
    stats enable
    stats hide-version
    stats uri     /admin
    stats realm   Haproxy\ Statistics
    stats auth    admin:admin
    stats admin if TRUE
frontend  main 
    bind *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js .html .ico
    use_backend static          if url_static
    default_backend             app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 10.1.249.75:6081 check
    rspadd X-Via:static
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server app 10.1.252.36:80 check
    rspadd X-Via:app

wKioL1gm-ZXjLE4tAAKt0g7HkT8868.jpg

OK,haproxy+varnish實現動靜資源分離和靜態資源的緩存到此暫告一段落,更多內容請關注我的博客


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章