haproxy + varnish 实现动静分离

网络拓扑

image
image

准备工作:

禁用selinux,清空防火墙规则。
iptables -F
setenforce 0

配置各主机配置:

static server:

systemctl start httpd
image
image

dynamic server

yum install httpd php php-mysql php-mbstring php-mcrypt
vim /var/www/html/index.php     
Dynamic Server -----three        
<?php 
 phpinfo();
?>
image
image

nfs以及mysql服务器:

[root@centos7 ~]#yum  -y install nfs-server  nfs-utils mariadb-server
[root@centos7 ~]#systemctl start  nfs-server.service
[root@centos7 ~]#systemctl start  mariadb.service
[root@centos7 ~]#mysql_secure_installation  //进行mysql安全设置
[root@centos7 ~]#mysql -uroot -p123456 //以mysql的root身份登入。
MariaDB [(none)]> create database blogdb; //创建WordPress数据库
MariaDB [(none)]> grant all on blogdb.* to wpuser@'192.168.18.%' identified by '123456';  
//创建WordPress用户和密码 
下载WordPress并解压到、/app/blog下
[root@centos7 ~]#useradd -u 48  -r -s /sbin/nologin apache
[root@centos7 ~]#vim wp-config.php //直接对配置文件修改,
/** WordPress数据库的名称 */
define('DB_NAME', 'blogdb');
/** MySQL数据库用户名 */
define('DB_USER', 'wpuser');
/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');
/** MySQL主机 */
define('DB_HOST', '172.16.%。%');
[root@centos7 ~]#vim /etc/exports  //编辑nfs配置文件
/app/blog 192.168.18.0/24(rw,all_squash,anonuid=48,anongid=48)
挂载:

[root@centos7 ~]#vim /etc/fstab //写进配置文件。可以以后开机自动挂载。
192.168.18.103:/app/blog        /var/www/html/blog       nfs     defaults  0 0 //在最后添加这条记录
[root@centos7 ~]#mkdir /var/www/html/blog -pv 
[root@centos7 ~]#mount -a  
image
image

varnish主机:

Vim /etc/varnish/default.vcl
vcl 4.0;
import directors;   # 导入负载均衡模块
# Default backend definition. Set this to point to your content server.
probe healthchk {    # 配置健康状态检查
        .url = "/.healthchk.html";   # 检查状态检查的URL
        .timeout = 2s; # 超时时间
        .interval = 2s;# 每2秒检查一次
        .window = 8; # 一共检查的次数
        .threshold = 5; # 如果大于4次则为健康
}

backend appsrv1 {   # 配置后端主机
    .host = "172.16.251.240";
    .port = "80";
    .probe = healthchk;
}

haproxy 主机:

Vim /etc/haproxy/haproxy.cfg
    frontend  main
    bind        *:80
    rspadd          X-Via:\ HAProxy-1
    rspidel         Server.*
    acl static      path_end -i .html .css .js
    acl static      path_end -i .jpg .jpeg .gif .png
    acl static      path_beg -i /images /static
    use_backend     websrvs if static
    default_backend appsrvs
listen status
    bind *:9009
    acl auth_admin  src 192.168.52.1
    stats           enable
    stats uri       /myhaproxy?status
    stats realm     HAProxy\ Admin\ Area
    stats auth      admin:admin
    stats admin     if auth_admin
backend websrvs
    option      forwardfor header X-Client
    balance     uri
    server      web1    172.16.252.31:6081 check
    hash-type   consistent
backend appsrvs
    option      forwardfor header X-Client
    balance     uri
    server      app1     172.16.252.59:80 cookie app1 check
    hash-type   consistent
image
image
image
image

推荐阅读更多精彩内容