Varnish 2.1.5 Installation and Configuration on CentOS 6.x

           Varnishreverse proxysoftware,AboutPrinciple,VCLDescription,,《Varnish》

、InstallationCentOS5.8systemEnvironment

1
2
yum install gcc gcc-c++
yum install automake autoconflibtool ncurses-devel libxslt groff pcre-devel pkgconfig libtool -y

、Downloadvarnish-2.1.5,CompilationInstallation。

1
2
3
4
5
cd /usr/local/src
wget  http://repo.varnish-cache.org/source/varnish-2.1.5.tar.gz
tar zxvf varnish-2.1.5.tar.gz
cd varnish-2.1.5.
./autogen.sh

#autogen.shcommandchecksoftware,If,

1
2
3
4
5
+ aclocal
+ libtoolize --copy --force
+ autoheader
+ automake --add-missing --copy --foreign
+ autoconf

CompilationInstallation:

1
2
./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings -enable-extra-warnings
make && make install && cd ../

、CreatevarnishUser,varnishCachefileLogdirectory:

1
2
3
4
/usr/sbin/groupadd varnish
/usr/sbin/useradd -s /sbin/nologin  -g varnish varnish
mkdir -p /data/varnish/{cache,log}
chown  -R varnish:varnish /data/varnish/{cache,log}

、TestingEnvironmentWeb,IP192.168.1.103(http://www.yuhongchun027.net)varnishIP192.168.1.104192.168.1.105reverse proxy,Configuration/usr/local/varnish/etc/varnish/better.vclas shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
backend rserver1
{
.host ="192.168.1.104";
.port = "80";
.probe = {
.timeout = 5s;          #
.interval = 2s;          #
.window = 10;         #varnish10sliding windows
.threshold = 8;         #8.windows,Web
}
}
backend rserver2
{
.host ="192.168.1.105";
.port = "80";
.probe = {
.timeout = 5s;    
.interval = 2s;   
.window = 10;     
.threshold = 8;
}
}
#realserver,random,,,
;round-robin()weight
director realserver random {
{
.backend = rserver1;
.weight = 5;
}
{
.backend = rserver2;
.weight = 6;
}
}
#,purge
acl purge {
"localhost";
"127.0.0.1";
}
sub vcl_recv
{
  if (req.http.host ~"^(.*).yuhongchun027.net")
  {     
     set req.backend =realserver; 
  }  
     else
     {     
       error 200 "Nocahce for this domain"
     }           
       if (req.request =="PURGE")
         {        
           if (!client.ip ~purge)
             {           
                error 405"Not allowed.";        
             }
          else
             {
                return (pipe);
             }
}
#IPAddress
if(req.http.x-forwarded-for)
{         
set req.http.X-Forwarded-For =        
req.http.X-Forwarded-For "," client.ip;
}
else
{           
set req.http.X-Forwarded-For =client.ip;       
}
#HTTPGET、HEAD,POST,Web
。,POST,
、,;
if (req.request !="GET" && req.request != "HEAD")
{        
return (pipe);
}
if (req.http.Expect)
{       
return (pipe);
}
if (req.http.Authenticate|| req.http.Cookie)
{        
return (pass);
}
if (req.http.Cache-Control~ "no-cache")
{       
return (pass);
}
#JSPPHP
if(req.url ~"/.jsp" || req.url ~ "/.php" )
{        
return (pass);
}
else
{
return (lookup);
}
}sub vcl_pipe
{
return (pipe);
}sub vcl_pass
{
return (pass);
}sub vcl_hash
{
set req.hash += req.url;
if (req.http.host)
{  
set req.hash +=req.http.host;
}
else
set req.hash +=server.ip;
}
  return (hash);
}sub vcl_hit
{
if (req.request =="PURGE")
set obj.ttl = 0s;      
error 200"Purged.";
}
if (!obj.cacheable)
{  
return (pass);
}
return (deliver);
}sub vcl_miss
{
if (req.request =="PURGE")
{  
error 404 "Not incache.";
}
if (req.http.user-agent ~"spider")
{   
error 503 "Notpresently in cache";
}
     return (fetch);
}
sub vcl_fetch
{
if (req.request =="GET" && req.url ~ "/.(txt|js)$")
{  
set beresp.ttl = 3600s;
}
else
{  
set beresp.ttl = 30d;
}
if (!beresp.cacheable)
{  
return (pass);
}
if (beresp.http.Set-Cookie)
return (pass);
}
return (deliver);
}
sub vcl_deliver {
 if (obj.hits > 0) {
   set resp.http.X-Cache= "HIT  FROM www.yuhongchun027.net";
 } else {
   set resp.http.X-Cache= "MISS FROM www.yuhongchun027.net";
 }
return (deliver);
}

、Startvarnishcommand,as shown below:

 

1
2
3
/usr/local/varnish/sbin/varnishd -n /data/varnish/cache -f /usr/local/varnish/etc/varnish/better.vcl -a 0.0.0.0:80 -s
file,/data/varnish/varnish_cache.data,8G  -p user=varnish -p group=varnish -p default_ttl=14400 -p thread_pool_max=8000 -p send_timeout=20 -w
5,51200,30 -T 127.0.0.1:3500  -P /usr/local/varnish/var/varnish.pid

curl –Icommand,as shown below:

1
[root@localhost cache]# curl -I http://www.yuhongchun027.net/

varnishCacheFunction:

1
2
3
4
5
6
7
8
9
10
11
12
HTTP/1.1 200 OK
Server: Apache/2.2.3 (CentOS)
Last-Modified: Wed, 28 Aug 2013 16:27:33 GMT
ETag: "10d242-e-776b6740"
Content-Type: text/html; charset=UTF-8
Content-Length: 14
Date: Wed, 21 Aug 2013 17:47:48 GMT
X-Varnish: 1584727079 1584726982
Age: 10101
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT  FROM www.yuhongchun027.net

、Ifvclconfiguration file,Restartreload,,telnet,connection3500Managementport:

1
2
3
4
5
6
telnet 127.0.0.1 3500
vcl.load newconfig /usr/local/varnish/etc/varnish/better.vcl
200 13
VCL compiled.
vcl.use newconfig
200 0

If200,reload,newconfig,varnish,telnetconnectionCache。

、varnishadmcommandCache,as shown below:

Cache

1
/usr/local/varnish/bin/varnishadm -T 192.168.1.103:3500 url.purge *$

imagedirectoryCache

1
/usr/local/varnish/bin/varnishadm -T 192.168.1.103:3500 url.purge /image/

url,command:

1
/usr/local/varnish/bin/varnishadm –T 192.168.1.103:3500 purge.list

Additionally,CacheDescriptionvarnish,IfCache,varnishConfigurationcheck,commandas shown below:

1
/usr/local/varnish/bin/varnishstat -n /data/varnish/cache

、Optimizationas shown below:

/etc/sysctl.conf,Add:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

command,Configuration:

1
/sbin/sysctl –p

Note: :ulimitproblem,,,/etc/rc.localAdd:

1
ulimit –SHn 65535

Note: :Startvarnishcommand(MonitoringscriptserviceStartscript),Additionally,,CentOS6.X x86_64ulimitCentOS5.X x86_64,Note: 。varnish-2.1.5CentOS5.8 x86_64InstallationConfiguration,,,,Function。

This article is from “” Blog,http://andrewyu.blog.51cto.com/1604432/1293169

Leave a Comment

Your email address will not be published.