實驗環境:RHEL6.5
實驗環境4台
真機進行通路測試 willis.example.com 172.25.254.6
虛拟機1(緩存端) varnish.example.com 172.25.254.8
虛拟機2(伺服器端1) web1.example.com 172.25.254.10
虛拟機3(伺服器端2) web2.example.com 172.25.254.20
兩台伺服器主要用于負載均衡實驗。
實驗内容:1.伺服器端安裝Apache
2.代理端(緩存端)安裝配置varnish
3.緩存無法同步問題
4.虛拟主機
5.網頁重寫
6.負載均衡器
安裝包:varnish-3.0.5-1.el6.x86_64.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm
1.伺服器端安裝Apache
1.1 伺服器端1
[root@web1 ~]# yum install httpd -y
[root@web1 ~]# vim /var/www/html/index.html
web1's page
[root@web1 ~]# /etc/init.d/httpd start
1.2伺服器端2
[root@web2 ~]# yum install httpd -y
[root@web2 ~]# vim /var/www/html/index.html
web2's page
[root@web2 ~]# /etc/init.d/httpd start
2.代理端(緩存端)安裝配置varnish
[root@varnish mnt]# ls
varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[root@varnish mnt]# yum install * -y
[root@varnish mnt]# vim /etc/sysconfig/varnis
VARNISH_LISTEN_PORT=80 ##設定varnish的端口為80
[root@varnish mnt]# vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.254.10"; ##指定apache所在主機ip(本次實驗伺服器1/2端都可以)
.port = "80"; ##apache端口
}
[root@varnish mnt]# /etc/init.d/varnish start
Starting Varnish Cache: [ OK ]
[root@varnish mnt]# vim /etc/varnish/default.vcl ##設定緩存命中資訊
sub vcl_deliver{
if(obj.hits>0){
set resp.http.X-Cache="HIT from willis cache"; ##緩存命中
}
else{
set resp.http.X-Cache="MISS from willis cache"; ##緩存未命中
return(deliver);
}
[root@varnish mnt]# service varnish reload
Loading vcl from /etc/varnish/default.vcl
Current running config name is boot
Using new config name reload_2016-09-15T05:19:54
VCL compiled.
available 0 boot
active 0 reload_2016-09-15T05:19:54
Done
3.緩存無法同步問題
3.1 緩存端,通路緩存不過期頁面無法重新整理
[root@varnish ~]# curl http://172.25.254.8
web1's page
[root@varnish ~]# curl -I http://172.25.254.8
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 14 Sep 2016 21:02:59 GMT
ETag: "1fcb4-c-53c7e0dd4191f"
Content-Type: text/html; charset=UTF-8
Content-Length: 12
Accept-Ranges: bytes
Date: Wed, 14 Sep 2016 21:43:54 GMT
X-Varnish: 1470288177 1470288175
Age: 95
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from willis cache
Date: Wed, 14 Sep 2016 21:43:59 GMT
X-Varnish: 1470288178 1470288175
Age: 100
3.2 通過 varnishadm 手動清除緩存
# varnishadm ban.url .*$ #清除所有
# varnishadm ban.url /index.html #清除 index.html 頁面緩存
# varnishadm ban.url /admin/$ #清除 admin 目錄緩存
[root@varnish mnt]# curl -I http://172.25.254.8
Date: Wed, 14 Sep 2016 21:52:42 GMT
X-Varnish: 1470288192 1470288185
Age: 51
[root@varnish mnt]# varnishadm ban.url /index.html
Date: Wed, 14 Sep 2016 21:53:24 GMT
X-Varnish: 1470288193 1470288185
Age: 93
4.虛拟主機
4.1服務端1設定
[root@web1 ~]# vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
ServerAdmin /www/willis.com/html
DocumentRoot www.willis.com
</VirtualHost>
ServerAdmin /www/linux.com/html
DocumentRoot www.linux.com
[root@web1 ~]# mkdir -p /www/willis.com/html
[root@web1 ~]# mkdir -p /www/linux.com/html
[root@web1 ~]# echo "web1 willis's page ">/www/willis.com/html/index.html
[root@web1 ~]# echo "web1 linux's page ">/www/linux.com/html/index.html
[root@web1 ~]# /etc/init.d/httpd restart
4.2 服務端2設定
[root@web2 ~]# vim /etc/httpd/conf/httpd.conf
[root@web2 ~]# mkdir -p /www/willis.com/html
[root@web2 ~]# mkdir -p /www/linux.com/html
[root@web2 ~]# echo "web2 willis's page ">/www/willis.com/html/index.html
[root@web2 ~]# echo "web2 linux's page ">/www/linux.com/html/index.html
[root@web2 ~]# /etc/init.d/httpd restart
4.3通路端測試
[root@willis ~]# vim /etc/hosts
172.25.254.8 www.willis.com
172.25.254.8 www.linux.com
###注意,前面指定 .host = "172.25.254.10",是以通路結果為伺服器1端的網頁内容

5.網頁重寫
backend web1 {
.host = "172.25.254.10";
.port = "80";
}
backend web2 {
.host="172.25.254.20";
.port="80";
sub vcl_recv { ##網頁緩存
if (req.http.host ~ "^(www.)?willis.com" ) { ##通路中是否帶www
set req.http.host = "www.willis.com"; ##都定向到www上
set req.backend = web1; ##通路web1
elsif (req.http.host ~ "^(www.)?linux.com" ) {
set req.http.host = "www.linux.com";
set req.backend = web1;
else {error 404 "westos cache";
[root@varnish mnt]# /etc/init.d/varnish restart
通路端測試
通路 willis.com自動跳轉成www.willis.com
通路 linux.com自動跳轉成www.linux.com
6.負載均衡器
已經提前配好好兩台伺服器,本實驗隻需配置緩存端
director willislb round-robin {
{ .backend = web1; }
{ .backend = web2; }
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.com" ) {
set req.http.host = "www.westos.com";
set req.backend = willislb; ##修改
return (pass); ##友善測試,不緩存
return (pass); ##友善測試,不緩存
通路端測試:
linux.com或者willis.com 網頁重寫,重新整理可看到内容的變化
重新整理: