介紹使用HAProxy+varnish實(shí)現(xiàn)WordPress的動(dòng)靜分離;
實(shí)驗(yàn)要求:
(1) 動(dòng)靜分離部署wordpress白筹,動(dòng)靜都要能實(shí)現(xiàn)負(fù)載均衡智末,要注意會(huì)話(huà)的問(wèn)題;
(2) 在haproxy和后端主機(jī)之間添加varnish進(jìn)行緩存徒河;
(3) haproxy的設(shè)定要求:
(a) stats page系馆,要求僅能通過(guò)本地訪(fǎng)問(wèn)使用管理接口;
(b) 動(dòng)靜分離虚青;
(c) 分別考慮不同的服務(wù)器組的調(diào)度算法它呀;
如需實(shí)現(xiàn)高可用,亦可在HAproxy代理服務(wù)器上實(shí)現(xiàn)keeplived高可用棒厘,使用兩臺(tái)varnish緩存纵穿。
實(shí)驗(yàn)環(huán)境:
使用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靜態(tài)端服務(wù):
yum -y install httpd
echo " web Server PAGE" > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
測(cè)試是否正常工作
curl http://192.168.18.99
web Server PAGE
二、部署動(dòng)態(tài)端服務(wù)
yum install httpd php php-mysql php-mbstring php-mcrypt
vim /var/www/html/index.php
Dynamic Server
<?php
phpinfo();
?>
測(cè)試是否正常運(yùn)行:三奢人、配置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 //進(jìn)行mysql安全設(shè)置
[root@centos7 ~]#mysql -uroot -p123456 //以mysql的root身份登入谓媒。
MariaDB [(none)]> create database blogdb; //創(chuàng)建WordPress數(shù)據(jù)庫(kù)
MariaDB [(none)]> grant all on blogdb.* to wpuser@'192.168.18.%' identified by '123456';
//創(chuàng)建WordPress用戶(hù)和密碼
下載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 //直接對(duì)配置文件修改何乎,
/** WordPress數(shù)據(jù)庫(kù)的名稱(chēng) */
define('DB_NAME', 'blogdb');
/** MySQL數(shù)據(jù)庫(kù)用戶(hù)名 */
define('DB_USER', 'wpuser');
/** MySQL數(shù)據(jù)庫(kù)密碼 */
define('DB_PASSWORD', '123456');
/** MySQL主機(jī) */
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 //寫(xiě)進(jìn)配置文件≈Ь龋可以以后開(kāi)機(jī)自動(dòng)掛載抢野。
192.168.18.103:/app/blog /var/www/html/blog nfs defaults 0 0 //在最后添加這條記錄
[root@centos7 ~]#mkdir /var/www/html/blog -pv //沒(méi)有此目錄創(chuàng)建此目錄。
[root@centos7 ~]#mount -a //對(duì)/etc/fstab 新增內(nèi)容掛載
測(cè)試:三各墨、配置安裝 HAProxy
yum -y install haproxy
vim /etc/haproxy.cfg
frontend main
# 監(jiān)聽(tīng)在80端口
bind *:80
# 增加自定義頭部
rspadd X-Via:\ HAProxy-1
# 刪除頭部
rspidel Server.*
# ACL規(guī)則
acl static path_end -i .html .css .js
acl static path_end -i .jpg .jpeg .gif .png
acl static path_beg -i /images /static
# 如果滿(mǎn)足定義的static ACL規(guī)則指孤,則調(diào)度至此后端
use_backend websrvs if static
# 默認(rèn)后端
default_backend appsrvs
listen status
# 管理頁(yè)面監(jiān)聽(tīng)端口
bind *:9009
# ACL規(guī)則
acl auth_admin src 192.168.18.1
# 開(kāi)啟狀態(tài)頁(yè)
stats enable
# 狀態(tài)頁(yè)URI
stats uri /myhaproxy?status
# 狀態(tài)頁(yè)提示信息
stats realm HAProxy\ Admin\ Area
# 認(rèn)證用戶(hù):密碼
stats auth admin:admin
# 如果滿(mǎn)足 auth_admin條件則允許訪(fǎng)問(wèn)狀態(tài)頁(yè)
stats admin if auth_admin
backend websrvs
# 添加頭部,日志中可以使用
option forwardfor header X-Client
# 負(fù)載均衡調(diào)度算法為 URI
balance uri
# 后端服務(wù)器贬堵,健康檢查恃轩、權(quán)重、自定義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; # 導(dǎo)入負(fù)載均衡模塊
# Default backend definition. Set this to point to your content server.
probe healthchk { # 配置健康狀態(tài)檢查
.url = "/.healthchk.html"; # 檢查狀態(tài)檢查的URL
.timeout = 2s; # 超時(shí)時(shí)間
.interval = 2s;# 每2秒檢查一次
.window = 8; # 一共檢查的次數(shù)
.threshold = 5; # 如果大于4次則為健康
}
backend appsrv1 { # 配置后端主機(jī)
.host = "192.168.18.100";
.port = "80";
.probe = healthchk;
}
backend websrv1 { # 配置后端主機(jī)
.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 { # 初始化負(fù)載均衡
new websrvs = directors.round_robin();
websrvs.add_backend(websrv1);
}
sub vcl_recv { # 定義接收段
# 如果請(qǐng)求的URL中包括以下信息叉跛,則調(diào)度至我們的后端主機(jī)
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;
}
## 如果請(qǐng)求方法是PURGE,也就是裁剪緩存
if (req.method == "PURGE") {
# 如果客戶(hù)端IP不在我們之前定義的ACL for purges中蒸殿,提示如下信息
if (!client.ip ~ purgers) {
return(synth(405,"Purging not allowed for " + client.ip));
}
return(purge); # 反之筷厘,執(zhí)行裁剪緩存
}
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;
}
}
# 如果相應(yīng)的狀態(tài)碼不是200或者404,則不緩存
if ( beresp.status != 200 && beresp.status != 404 ) {
set beresp.uncacheable = true;
set beresp.ttl = 120s;
return (deliver);
}
# 設(shè)置默認(rèn)ttl緩存為 1小時(shí)
set beresp.ttl = 1h;
}
sub vcl_purge { # 定義裁剪緩存的提示信息
return (synth(200,"Purged."));
}
sub vcl_deliver {
# 如果命中了則返回自定義頭部宏所,未命中則返回另一個(gè)自定義頭部
if (obj.hits > 0) {
set resp.http.X-Cache = " Hit via " + server.ip;
} else {
set resp.http.X-Cache = " Miss via " + server.ip;
}
}
到這里就配置完成了:
測(cè)試: