介绍使用HAProxy+varnish实现WordPress的动静分离;
实验要求:
(1) 动静分离部署wordpress,动静都要能实现负载均衡,要注意会话的问题;
(2) 在haproxy和后端主机之间添加varnish进行缓存;
(3) haproxy的设定要求:
(a) stats page,要求仅能通过本地访问使用管理接口;
(b) 动静分离;
(c) 分别考虑不同的服务器组的调度算法;
如需实现高可用,亦可在HAproxy代理服务器上实现keeplived高可用,使用两台varnish缓存。
实验环境:
使用centos7.3
HAproxy ip:192.168.18.97
varnish ip 192.168.18.98
static server ip :192.168.18.99
Dynamic server ip :192.168.18.100
mysql+nfs ip :192.168.18.103
一.部署web静态端服务:
yum -y install httpd
echo " web Server PAGE" > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
测试是否正常工作
curl http://192.168.18.99
web Server PAGE
二、部署动态端服务
yum install httpd php php-mysql php-mbstring php-mcrypt
vim /var/www/html/index.php
Dynamic Server
<?php
phpinfo();
?>
测试是否正常运行:
三、配置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 ~]#chown -R apache.apache /app/blog/
[root@centos7 ~]#cp /app/blog/wp-config-sample.php /app/blog/wp-config.php
[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', '192.168.18.103');
[root@centos7 ~]#vim /etc/exports //编辑nfs配置文件
/app/blog 192.168.18.0/24(rw,all_squash,anonuid=48,anongid=48)
挂载:
在static server和Dynamic server做挂载。
[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 //对/etc/fstab 新增内容挂载
测试:
三、配置安装 HAProxy
yum -y install haproxy
vim /etc/haproxy.cfg
frontend main
# 监听在80端口
bind *:80
# 增加自定义头部
rspadd X-Via:\ HAProxy-1
# 删除头部
rspidel Server.*
# ACL规则
acl static path_end -i .html .css .js
acl static path_end -i .jpg .jpeg .gif .png
acl static path_beg -i /images /static
# 如果满足定义的static ACL规则,则调度至此后端
use_backend websrvs if static
# 默认后端
default_backend appsrvs
listen status
# 管理页面监听端口
bind *:9009
# ACL规则
acl auth_admin src 192.168.18.1
# 开启状态页
stats enable
# 状态页URI
stats uri /myhaproxy?status
# 状态页提示信息
stats realm HAProxy\ Admin\ Area
# 认证用户:密码
stats auth admin:admin
# 如果满足 auth_admin条件则允许访问状态页
stats admin if auth_admin
backend websrvs
# 添加头部,日志中可以使用
option forwardfor header X-Client
# 负载均衡调度算法为 URI
balance uri
# 后端服务器,健康检查、权重、自定义cookie
server web1 192.168.18.98:80 check cookie web1
# 一致性HASH
hash-type consistent
backend appsrvs
option forwardfor header X-Client
balance uri
#balance roundrobin
server app1 192.168.18.99:80 cookie app1 check
hash-type consistent
四、配置varnish
yum -y install varnish
cp /etc/varnish/varnish.params{,.bak}
vim /etc/varnish/varnish.params
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 = "192.168.18.100";
.port = "80";
.probe = healthchk;
}
backend websrv1 { # 配置后端主机
.host = "192.168.18.99";
.port = "80";
.probe = healthchk;
}
acl purgers { # 定义裁剪的ACL
"127.0.0.0"/8;
"192.168.18.131"/32;
}
acl baner {
"127.0.0.1"/8;
}
sub vcl_init { # 初始化负载均衡
new websrvs = directors.round_robin();
websrvs.add_backend(websrv1);
}
sub vcl_recv { # 定义接收段
# 如果请求的URL中包括以下信息,则调度至我们的后端主机
if (req.url ~ "(?i)\.(jpg|jpeg|png|gif|svg|txt|html|css|js)$") {
set req.backend_hint = websrvs.backend();
}else {
set req.backend_hint = appsrv1;
}
## 如果请求方法是PURGE,也就是裁剪缓存
if (req.method == "PURGE") {
# 如果客户端IP不在我们之前定义的ACL for purges中,提示如下信息
if (!client.ip ~ purgers) {
return(synth(405,"Purging not allowed for " + client.ip));
}
return(purge); # 反之,执行裁剪缓存
}
if (req.method == "BAN") {
if (!client.ip ~ baner) {
return(synth(405,"baning not allowed for " + client.ip));
}
ban("req.http.host == " + req.http.host + " && req.url == " + req.url);
return (synth(200,"Ban added"));
}
if (req.method == "PURGE") {
return(purge);
}
# 自定义头部
if (req.restarts == 0) {
if (req.http.X-Fowarded-For) {
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + "," + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}
# 如果相应的状态码不是200或者404,则不缓存
if ( beresp.status != 200 && beresp.status != 404 ) {
set beresp.uncacheable = true;
set beresp.ttl = 120s;
return (deliver);
}
# 设置默认ttl缓存为 1小时
set beresp.ttl = 1h;
}
sub vcl_purge { # 定义裁剪缓存的提示信息
return (synth(200,"Purged."));
}
sub vcl_deliver {
# 如果命中了则返回自定义头部,未命中则返回另一个自定义头部
if (obj.hits > 0) {
set resp.http.X-Cache = " Hit via " + server.ip;
} else {
set resp.http.X-Cache = " Miss via " + server.ip;
}
}
到这里就配置完成了:
测试:
网友评论