標籤:虛擬機器主機 伺服器 加速器 虛擬機器 均衡器 apache+varnish
實驗環境: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
[[email protected] ~]# yum install httpd -y
[[email protected] ~]# vim /var/www/html/index.html
web1‘s page
[[email protected] ~]# /etc/init.d/httpd start
1.2伺服器端2
[[email protected] ~]# yum install httpd -y
[[email protected] ~]# vim /var/www/html/index.html
web2‘s page
[[email protected] ~]# /etc/init.d/httpd start
2.代理端(緩衝端)安裝配置varnish
[[email protected] mnt]# ls
varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[[email protected] mnt]# yum install * -y
[[email protected] mnt]# vim /etc/sysconfig/varnis
VARNISH_LISTEN_PORT=80##設定varnish的連接埠為80
[[email protected] mnt]# vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.254.10"; ##指定apache所在主機ip(本次實驗伺服器1/2端都可以)
.port = "80"; ##apache連接埠
}
[[email protected] mnt]# /etc/init.d/varnish start
Starting Varnish Cache: [ OK ]
[[email protected] 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);
}
[[email protected] 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 緩衝端,訪問緩衝不到期頁面無法重新整理
[[email protected] ~]# curl http://172.25.254.8
web1‘s page
[[email protected] ~]# 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
[[email protected] ~]# 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:59 GMT
X-Varnish: 1470288178 1470288175
Age: 100
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from willis cache
3.2 通過 varnishadm 手動清除緩衝
# varnishadm ban.url .*$ #清除所有
# varnishadm ban.url /index.html #清除 index.html 頁面緩衝
# varnishadm ban.url /admin/$#清除 admin 目錄緩衝
[[email protected] mnt]# 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:52:42 GMT
X-Varnish: 1470288192 1470288185
Age: 51
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from willis cache
[[email protected] mnt]# varnishadm ban.url /index.html
[[email protected] mnt]# 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:53:24 GMT
X-Varnish: 1470288193 1470288185
Age: 93
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from willis cache
4.虛擬機器主機
4.1服務端1設定
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
ServerAdmin /www/willis.com/html
DocumentRoot www.willis.com
</VirtualHost>
<VirtualHost *:80>
ServerAdmin /www/linux.com/html
DocumentRoot www.linux.com
</VirtualHost>
[[email protected] ~]# mkdir -p /www/willis.com/html
[[email protected] ~]# mkdir -p /www/linux.com/html
[[email protected] ~]# echo "web1 willis‘s page ">/www/willis.com/html/index.html
[[email protected] ~]# echo "web1 linux‘s page ">/www/linux.com/html/index.html
[[email protected] ~]# /etc/init.d/httpd restart
4.2 服務端2設定
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
ServerAdmin /www/willis.com/html
DocumentRoot www.willis.com
</VirtualHost>
<VirtualHost *:80>
ServerAdmin /www/linux.com/html
DocumentRoot www.linux.com
</VirtualHost>
[[email protected] ~]# mkdir -p /www/willis.com/html
[[email protected] ~]# mkdir -p /www/linux.com/html
[[email protected] ~]# echo "web2 willis‘s page ">/www/willis.com/html/index.html
[[email protected] ~]# echo "web2 linux‘s page ">/www/linux.com/html/index.html
[[email protected] ~]# /etc/init.d/httpd restart
4.3訪問端測試
[[email protected] ~]# vim /etc/hosts
172.25.254.8 www.willis.com
172.25.254.8 www.linux.com
###注意,前面指定 .host = "172.25.254.10",所以訪問結果為伺服器1端的網頁內容
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/87/46/wKioL1faU-KyTHLYAAAhZPMhP5k541.png" style="float:none;" title="Screenshot from 2016-09-15 14:50:25.png" alt="wKioL1faU-KyTHLYAAAhZPMhP5k541.png" />
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/87/46/wKioL1faU-WyeXl8AAAhKRDWon0175.png" style="float:none;" title="Screenshot from 2016-09-15 15:49:01.png" alt="wKioL1faU-WyeXl8AAAhKRDWon0175.png" />
5.網頁重寫
[[email protected] mnt]# vim /etc/varnish/default.vcl
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";
}
}
[[email protected] mnt]# /etc/init.d/varnish restart
訪問端測試
訪問 willis.com自動跳轉成www.willis.com
訪問 linux.com自動跳轉成www.linux.com
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/87/46/wKioL1faU-KyTHLYAAAhZPMhP5k541.png" title="Screenshot from 2016-09-15 14:50:25.png" style="float:none;" alt="wKioL1faU-KyTHLYAAAhZPMhP5k541.png" />
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/87/46/wKioL1faU-WyeXl8AAAhKRDWon0175.png" title="Screenshot from 2016-09-15 15:49:01.png" style="float:none;" alt="wKioL1faU-WyeXl8AAAhKRDWon0175.png" />
6.負載平衡器
已經提前配好好兩台伺服器,本實驗只需配置緩衝端
[[email protected] mnt]# vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.254.10";
.port = "80";
}
backend web2 {
.host="172.25.254.20";
.port="80";
}
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); ##方便測試,不緩衝
}
elsif (req.http.host ~ "^(www.)?linux.com" ) {
set req.http.host = "www.linux.com";
set req.backend = willislb; ##修改
return (pass); ##方便測試,不緩衝
}
else {error 404 "westos cache";
}
}
訪問端測試:
linux.com或者willis.com 網頁重寫,重新整理可看到內容的變化
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/87/49/wKiom1faUtjwuC9YAAAh4KO8gYo192.png" style="float:none;" title="Screenshot from 2016-09-15 15:49:39.png" alt="wKiom1faUtjwuC9YAAAh4KO8gYo192.png" />
重新整理:
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/87/49/wKiom1faUt3j1db6AAAioPWradI001.png" style="float:none;" title="Screenshot from 2016-09-15 15:49:30.png" alt="wKiom1faUt3j1db6AAAioPWradI001.png" />
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/87/46/wKioL1faUt_iMNYyAAAhPqa139Y330.png" style="float:none;" title="Screenshot from 2016-09-15 15:49:11.png" alt="wKioL1faUt_iMNYyAAAhPqa139Y330.png" />
重新整理:
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/87/46/wKioL1faUt_CkmwuAAAhKRDWon0258.png" style="float:none;" title="Screenshot from 2016-09-15 15:49:01.png" alt="wKioL1faUt_CkmwuAAAhKRDWon0258.png" />
本文出自 “技術人生,簡單不簡單” 部落格,請務必保留此出處http://willis.blog.51cto.com/11907152/1852967
Apache+varnish(高效能開源HTTP加速器)搭建負載平衡叢集