實驗環境
- varnish ——172.25.54.5 server1(虛擬機test1)
- apache ——172.25.54.6 server2(虛擬機test2)
- apache ——172.25.54.7 server3(虛擬機test3)
每個實驗都在前一個實驗的基礎上
實驗一、簡單實現varnish
配置varnish服務器(server1)
- 安裝varnish yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y
varnish 安裝包 - 配置varnish服務器server1
[root
@server1 ~]
[root
@server1 ~]
[root
@server1 varnish]
default.vcl secret
[root
@server1 varnish]
-rw-r--r--
1 root root
3119 Dec
2 2013 default.vcl
[root
@server1 varnish]
-rw-------
1 root root
37 Jul
28 14:
00 secret
[root
@server1 varnish]
7 backend
default {
8 .host =
"172.25.54.6";
9 .port =
"80";
10 }
[root
@server1 varnish]
66 VARNISH_LISTEN_PORT=
80
[root
@server1 varnish]
fs.file-nr =
512 0 98862
fs.file-max =
98862
[root
@server1 varnish]
[root
@server1 security]
-rwxr-xr-x.
1 root root
18592 Oct
7 2013 pam_limits.so
[root
@server1 security]
52 varnish - nofile
9800053 varnish - memlock
8200054 varnish - nproc unlimited
[root
@server1 security]
Starting Varnish
Cache: [ OK ]
[root
@server1 security]
[root
@server1 varnish]
Loading vcl from /etc/varnish/
default.vcl
Current running config name
is reload_2018-
07-
28T14:
32:
35
Using
new config name reload_2018-
07-
28T14:
33:
25
VCL compiled.available
0 boot
available
0 reload_2018-
07-
28T14:
32:
35
active
0 reload_2018-
07-
28T14:
33:
25Done
[root
@server1 varnish]
[root@server2 html]
[root@server2 ~]
Starting httpd: httpd: Could
not reliably determine
the server's fully qualified domain
name, using
172.25.54.2 for ServerName[ OK ]
[root@server2 ~]
[root@server2 html]
[root@server2 html]
www.westos.org - server2
[root@server2 html]
[root@foundation54 ~]
# vim /etc/hosts
[root@foundation54 ~]
# cat /etc/hosts
127.0.0.1 localhost localhost
.localdomain localhost4 localhost4
.localdomain4
::
1 localhost localhost
.localdomain localhost6 localhost6
.localdomain6
172.25.54.5 www
.westos.org bbs
.westos.org westos
.org
[root@foundation54 ~]
# curl www.westos.org
<h1>www
.westos.org - server2</h1>
[root@foundation54 ~]
#
實驗二、查看緩存情況
[root
@server1 varnish]
12 sub vcl_deliver {13 if (obj.hits >
0) {
14 set resp.http.X-Cache =
"HIT from westos cache";
15 }
16 else {
17 set resp.http.X-Cache =
"MISS from westos cache";
18 }
19 return (deliver);
20 }
[root
@server1 varnish]
Stopping Varnish Cache: [ OK ]
[root
@server1 varnish]
Starting Varnish Cache: [ OK ]
[root
@server1 varnish]
Loading vcl from /etc/varnish/default.vcl
Current running config name is boot
Using new config name reload_2018-
07-
28T14:
58:
29
VCL compiled.available
0 boot
active
0 reload_2018-
07-
28T14:
58:
29Done
[root
@server1 varnish]
- 真機測試緩存命中
清除緩存
[root@server1 varnish]# varnishadm ban.url /index.html
再測試
沒有改變,是因為存在瀏覽器緩存的問題,用 curl 測試
清除緩存
[root@server1 varnish]# varnishadm ban.url /index.html
實驗三、定義多個不同域名站點的后端服務器
[root@server1 varnish]
# vim default.vcl ##定義多個不同域名站點的后端服務器7 backend web1 {
8 .host =
"172.25.54.6"9 .port =
"80"10 }
11 12 backend web2 {
13 .host =
"172.25.54.7"14 .port =
"80"15 }
##當訪問 www.westos.org 域名時從 web1 上取數據,##訪問 bbs.westos.org 域名時到 web2 取數據,##訪問其他頁面報錯17 sub vcl_recv {
18 if (req
.http.host ~
"^(www.)?westos.org") {
19 set req
.http.host =
"www.westos.org"20 set req
.backend = web1
21 } elsif (req
.http.host ~
"^bbs.westos.org") {
22 set req
.backend = web2
23 } else {error
404 "westos cache"24 }
25 }
26 27 sub vcl_deliver {
28 if (obj
.hits >
0) {
29 set resp
.http.X-Cache =
"HIT from westos cache"30 }
31 else {
32 set resp
.http.X-Cache =
"MISS from westos cache"33 }
34 return (deliver)
35 }
[root@server1 varnish]
# /etc/init.d/varnish reload
Loading vcl from /etc/varnish/default
.vcl
Current running config name is reload_2018-
07-
28T14:
58:
29
Using new config name reload_2018-
07-
28T15:
19:
24
VCL compiled.available
0 boot
available
2 reload_2018-
07-
28T14:
58:
29
active
0 reload_2018-
07-
28T15:
19:
24Done
[root@server1 varnish]
#
[root@server3 ~]
[root@server3 ~]
Starting httpd: httpd: Could
not reliably determine
the server's fully qualified domain
name, using
172.25.54.3 for ServerName[ OK ]
[root@server3 ~]
[root@server3 html]
[root@server3 html]
[root@server3 html]
bbs.westos.org
[root@server3 html]
實驗四、把多個后端聚合為一個組
[root@server3 html]
# vim /etc/httpd/conf/httpd.conf 990 NameVirtualHost *:
80
1010 <VirtualHost *:
80>
1011 DocumentRoot /var/www/html
1012 ServerName bbs
.westos.org
1013 </VirtualHost>
1014
1015 <VirtualHost *:
80>
1016 DocumentRoot /www1
1017 ServerName www
.westos.org
1018 </VirtualHost>
[root@server3 html]
# mkdir /www1
[root@server3 html]
# cd /www1/
[root@server3 www1]
# ls
[root@server3 www1]
# vim index.html
[root@server3 www1]
# cat index.html
www
.westos.org - server3
[root@server3 www1]
# /etc/init.d/httpd restart
[root@server3 www1]
# vim /etc/hosts
[root@server3 www1]
# cat /etc/hosts
127.0.0.1 localhost localhost
.localdomain localhost4 localhost4
.localdomain4
::
1 localhost localhost
.localdomain localhost6 localhost6
.localdomain6
172.25.54.7 server7 www
.westos.org bbs
.westos.org
[root@server3 www1]
# curl www.westos.org
www
.westos.org - server3
[root@server3 www1]
# curl bbs.westos.org
bbs
.westos.org
[root@server3 www1]
#
[root@server1 varnish]
# vim default.vcl 17 director tutu round-robin {
18 {
.backend = web1
19 {
.backend = web2
20 }
21 22 sub vcl_recv {
23 if (req
.http.host ~
"^(www.)?westos.org") {
24 set req
.http.host =
"www.westos.org"25 set req
.backend = tutu
26 return (pass)
27 } elsif (req
.http.host ~
"^bbs.westos.org") {
28 set req
.backend = web2
29 } else {error
404 "westos cache"30 }
31 }
[root@server1 varnish]
# /etc/init.d/varnish reload
Loading vcl from /etc/varnish/default
.vcl
Current running config name is reload_2018-
07-
28T15:
19:
24
Using new config name reload_2018-
07-
28T15:
50:
54
VCL compiled.available
0 boot
available
0 reload_2018-
07-
28T14:
58:
29
available
2 reload_2018-
07-
28T15:
19:
24
active
0 reload_2018-
07-
28T15:
50:
54Done
[root@server1 varnish]
#
[root@foundation54 ~]
# curl www.westos.org
www
.westos.org - server2
[root@foundation54 ~]
# curl www.westos.org
www
.westos.org - server3
[root@foundation54 ~]
# curl www.westos.org
www
.westos.org - server2
[root@foundation54 ~]
# curl www.westos.org
www
.westos.org - server3
[root@foundation54 ~]
# curl www.westos.org
www
.westos.org - server2
[root@foundation54 ~]
# curl www.westos.org
www
.westos.org - server3
[root@foundation54 ~]
#
實驗五、varnish cdn 推送平臺
[root
@server1 ~]
[root
@server1 ~]
bansys.zip
[root
@server1 ~]
[root
@server1 ~]
[root
@server1 html]
bansys
[root
@server1 html]
[root
@server1 bansys]
class_socket.php config.php
index.php purge_action.php static
[root
@server1 bansys]
[root
@server1 bansys]
[root
@server1 bansys]
[root
@server1 html]
bansys class_socket.php config.php
index.php purge_action.php static
[root
@server1 html]
[root
@server1 html]
[root
@server1 html]
136 Listen 8080
[root
@server1 html]
[root
@server1 html]
Active Internet connections (servers
and established)
Proto Recv-
Q Send-
Q Local Address Foreign Address State PID/
Program name
tcp
0 0 :::8080 :::* LISTEN 1422/httpd
[root@server1 html]
# cd /etc/varnish/
[root@server1 varnish]
# ls
default
.vcl secret
[root@server1 varnish]
# vim default.vcl
##添加7 acl westos {
8 "127.0.0.1"9 "172.25.54.0"/
2410 }
sub vcl_recv {
28 ##在函數內添加29 if (req
.request ==
"BAN") {
30 if (!client
.ip ~ westos) {
31 error
405 "Not allowed."32 }
33 ban(
"req.url ~ " + req
.url)
34 error
200 "ban added"35 }
36 #####37 if (req
.http.host ~
"^(www.)?westos.org") {
38 set req
.http.host =
"www.westos.org"39 set req
.backend = tutu
41 } elsif (req
.http.host ~
"^bbs.westos.org") {
42 set req
.backend = web2
43 } else {error
404 "westos cache"44 }
45 }
[root@server1 varnish]
# /etc/init.d/varnish reload
Loading vcl from /etc/varnish/default
.vcl
Current running config name is boot
Using new config name reload_2018-
07-
29T09:
43:
47
VCL compiled.available
2 boot
active
0 reload_2018-
07-
29T09:
43:
47Done
[root@server1 varnish]
#
瀏覽器頁面未改變是因為瀏覽器緩存導致的
[root@foundation54 ~]
# curl www.westos.org/index.html
<h1>www
.westos.org - server3</h1>
[root@foundation54 ~]
# curl www.westos.org/index.html
<h1>www
.westos.org - server3</h1>
[root@foundation54 ~]
# curl www.westos.org/index.html
<h1>www
.westos.org - server3</h1>
[root@foundation54 ~]
#
[root@foundation54 ~]
# curl www.westos.org/index.html
<h1>www
.westos.org - server2</h1>
[root@foundation54 ~]
# curl www.westos.org/index.html
<h1>www
.westos.org - server2</h1>
[root@foundation54 ~]
# curl www.westos.org/index.html
<h1>www
.westos.org - server2</h1>
[root@foundation54 ~]
#
總結
以上是生活随笔為你收集整理的varnish的服务配置(实验说明)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。