Varnish is a high-performance open source HTTP accelerator, Norway's largest online newspaper Verdens Gang use 3 units varnish replaced the original 12 squid, better performance than before.
But compared with the old squid, each has its own advantages and disadvantages, the Internet a large number of relative is only in their own familiar application of the largest use of the play only, may squid to have the ability of the hands to play the most powerful power
Varnish uses the "Visual Page Cache" technology, in the use of memory, varnish than squid has advantages, it avoids squid frequently in memory, disk Exchange files, performance than squid high.
With the varnish management port, you can use regular expressions to clear part of the cache quickly and in bulk, something squid cannot have.
I will make a brief introduction and notes on some ideas and collocation methods of varnish
experimental Environment: Red Hat Enterprise Linux Server Release 5.4 (Tikanga)
Kernel 2.6.18-164.el5
Yum Install Pcre-devel # #预先安装一个软件包, or you'll be prompted for an error
Tar zxvf varnish-2.1.3.tar.gz
CD varnish-2.1.3
./configure--prefix=/usr/local/varnish-2.1.3
Make && make install
Edit the configuration file, there are templates, but too many comments, it is best to create a new
Vim/usr/local/varnish-2.1.3/etc/varnish/varnish.conf
########### #下面附上配置文件的内容及注释 #######################
#http请求处理过程
#1, receive request entry status, Judge Pass or lookup local query according to VCL
#lookup, look for the data in the hash table, if found then enter the hit state, otherwise enter the fetch state
#pass, select Background, enter the fetch status
#fetch, get the request back, send the request, get the data, and store it locally
#deliver, send the data to the client and enter the done
#done, end of processing
######### #配置后端服务器 ##############
Copy Code code as follows:
Backend LINUXIDC01 {
. Host = "192.168.1.142";
. Port = "7070";
. Probe = {
. Timeout = 5s;
. interval = 2s;
. window = 10;
. threshold = 8;
}
}
Backend LINUXIDC02 {
. Host = "192.168.1.141";
. Port = "7070";
. Probe = {
. Timeout = 5s;
. interval = 2s;
. window = 10;
. threshold = 8;
}
}
############# #配置后端服务器组, Carry on health examination 6 seconds, use random way to set weights ########
######## #另一种方式round-robin The default polling mechanism ####################
Copy Code code as follows:
Director linuxidc15474 Random
{. retries = 6;
{. backend = LINUXIDC02;
. Weight = 2;
}
{. backend = LINUXIDC01;
. Weight = 2;
}
}
######### #定义访问列表, allow the following addresses to clear the varnish cache #######################
Copy Code code as follows:
ACL Local {
"LocalHost";
"127.0.0.1";
}
####### #从url判断针对哪类后面服务器及缓存配置 ############################
Copy Code code as follows:
Sub Vcl_recv
{
if (req.http.host ~ "^linuxidc15474.vicp.net") #匹配域名跳转后台服务器
{Set req.backend = linuxidc15474;}
else {error 404 "Unknown hostname!";}
if (req.request = = "PURGE") #不允许非访问控制列表内的IP清除varnish缓存
{if (!client.ip ~ local)
{
Error 405 "not allowed."
return (lookup);
}
}
#清除url中有jpg等文件的cookie
if (req.request = = "Get" && req.url ~ "\. Jpg|png|gif|swf|jpeg|ico) $ ")
{
Unset Req.http.cookie;
}
#判断req. http.x-forwarded-for If the front end has multiple reverse proxies, this can obtain the client IP address.
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;}
# #varnish实现图片的防盗链
# if (req.http.referer ~ "http://.*)
# {
# if (!) ( Req.http.referer ~ "Http://.*vicp\.net" | |
# req.http.referer ~ "Http://.*linuxidc15474\.net")
# {
# set req.http.host = "Linuxidc15474.vicp.net";
# set Req.url = "/referer.jpg";
# }
# return (lookup);
# }
# else {return (pass);}
if (req.request!= "Get" &&
Req.request!= "Head" &&
Req.request!= "put" &&
Req.request!= "POST" &&
Req.request!= "TRACE" &&
Req.request!= "Options" &&
Req.request!= "DELETE")
{return (pipe);}
#对非GET | Direct forwarding of head requests to back-end servers
if (req.request!= "get" && req.request!= "Head")
{return (pass);}
# #对GET请求, and at the end of the URL in. php and. PHP, go directly to the backend server
if (req.request = = "Get" && req.url ~ "\. PHP) ($|\?) ")
{return (pass);}
# #对请求中有验证及cookie, directly forwarded to the backend server
if (req.http.Authorization | | req.http.Cookie)
{return (pass);}
{
# #除以上的访问请求, looking up from the cache
return (lookup);
}
# #指定的font目录不进行缓存
if (req.url ~ "^/fonts/")
{return (pass);}
}
Sub Vcl_pipe
{return (pipe);}
# #进入pass模式, the request is sent to the backend, the backend returns the data to the client, but does not enter the cache processing
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);
}
# #在lookup后如果在cache中找到请求的缓存, usually end with a few keywords below
Sub Vcl_hit
{
if (!obj.cacheable)
{return (pass);}
return (deliver);
}
# #lookup后没有找到缓存时调用, end with the following keywords, and call fetch parameter to test whether to join the cache
Sub Vcl_miss
{return (fetch);}
#让varnish服务器缓存的类型, after obtaining data from the backend, call the
Sub Vcl_fetch
{if (!beresp.cacheable)
{return (pass);}
if (Beresp.http.set-cookie)
{return (pass);}
# #WEB服务器指明不缓存的内容, varnish server does not cache
if (beresp.http.Pragma ~ "No-cache" | | Beresp.http.cache-control ~ "No-cache" | | Beresp.http.cache-control ~ "Private")
{return (pass);}
# #对访问中get有包含jpg, PNG and other formats of the file cache, cache time is 7 days, s for seconds
if (req.request = = "Get" && req.url ~ "\. Js|css|mp3|jpg|png|gif|swf|jpeg|ico) $ ")
{Set beresp.ttl = 7d;}
# #对访问get中包含htm等静态页面, cache 300 seconds
if (req.request = "Get" && req.url ~ "\/[0-9]\.htm$")
{Set beresp.ttl = 300s;}
return (deliver);
}
### #添加在页面head头信息中查看缓存命中情况 ########
Sub Vcl_deliver
{
Set resp.http.x-hits = Obj.hits;
if (obj.hits > 0)
{Set Resp.http.x-cache = "HIT Cqtel-bbs";}
else {Set Resp.http.x-cache = "MISS Cqtel-bbs";}
}
######################## #以上为 varnish configuration file ##########################
To create a user:
Groupadd www
Useradd www-g www
To create a Varnish_cache cache location
Mkdir/data/varnish_cache
Start varnish
Ulimit-shn 8192 ### #设置文件描述符, because my machine performance is not good, you can follow their own configuration to set
/usr/local/varnish-2.1.3/sbin/varnishd-u www-g www-f/usr/local/varnish-2.1.3/etc/varnish/varnish.conf-a 0.0.0.0:80-s file,/data/varnish_cache/varnish_cache.data,100m-w 1024,8192,10-t 3600-t 127.0.0.1:3500
####-u with what group run-G to run the-F varnish configuration file-a bound IP and port-S varnish cache file location and size-W min, max thread and timeout-t varnish management port, mainly used to clear the cache
#结束varnishd进程
Pkill varnishd
The startup VARNISHNCSA is used to write the varnish access log to the log file:
/usr/local/varnish-2.1.3/bin/varnishncsa-w/data/logs/varnish.log &
Run at 0 o ' Day, cut varnish logs by day, generate a compressed file, and delete the script for the last month's Chronicles (/var/logs/cutlog.sh):
vim/usr/local/varnish-2.1.3/etc/varnish/cut_varnish_log.sh
Write the following script:
#!/bin/sh
# This file run at 00:00
date=$ (date-d "Yesterday" + "%y-%m-%d")
Pkill-9 VARNISHNCSA
Mv/data/logs/varnish.log/data/logs/${date}.log
/usr/local/varnish-2.1.3/bin/varnishncsa-w/data/logs/varnish.log &
Mkdir-p/data/logs/varnish/
Gzip-c/data/logs/${date}.log >/data/logs/varnish/${date}.log.gz
Rm-f/data/logs/${date}.log
Rm-f/data/logs/varnish/$ (date-d "-1 month" + "%y-%m*"). log.gz
Timed task:
Crontab-e
* * * */usr/local/varnish-2.1.3/etc/varnish/cut_varnish_log.sh
Optimizing Linux Kernel Parameters
Vi/etc/sysctl.conf
Net.ipv4.tcp_fin_timeout = 30
Net.ipv4.tcp_keepalive_time = 300
Net.ipv4.tcp_syncookies = 1
Net.ipv4.tcp_tw_reuse = 1
Net.ipv4.tcp_tw_recycle = 1
Net.ipv4.ip_local_port_range = 5000 65000
Make configuration effective
/sbin/sysctl-p
Bulk purge cache using regular expressions through the varnish management port
Clear all Caches
/usr/local/varnish-2.1.3/bin/varnishadm-t 127.0.0.1:3500 Url.purge *$
Clear all caches under Image directory
/usr/local/varnish-2.1.3/bin/varnishadm-t 127.0.0.1:3500 url.purge/image/
127.0.0.1:3500 for cleared cache server address www.linuxidc.com for cleared domain name/static/image/tt.jsp for cleared URL address list
/usr/local/varnish-2.1.3/bin/varnishadm-t 127.0.0.1:3500 purge "req.http.host ~ www.linuxidc.com$ && req.url ~ /static/image/tt.jsp "
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A PHP function that clears the squid cache
Copy Code code as follows:
<?php
function Purge ($IP, $url)
{
$errstr = ';
$errno = ';
$fp = Fsockopen ($ip, $errno, $ERRSTR, 2);
if (! $fp)
{
return false;
}
Else
{
$out = "PURGE $url http/1.1\r\n";
$out. = "host:blog.s135.com\r\n";
$out. = "connection:close\r\n\r\n";
Fputs ($fp, $out);
$out = Fgets ($fp, 4096);
Fclose ($FP);
return true;
}
}
Purge ("192.168.0.4", "/index.php");
?>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Configure Boot auto Start varnish
Vim/etc/rc.d/rc.local
Write the following at the end line:
Ulimit-shn 8192
/usr/local/varnish-2.1.3/sbin/varnishd-u www-g www-f/usr/local/varnish-2.1.3/etc/varnish/varnish.conf-a 0.0.0.0:80-s file,/data/varnish_cache/varnish_cache.data,100m-w 1024,8192,10-t 3600-t 127.0.0.1:3500
/usr/local/varnish-2.1.3/bin/varnishncsa-w/data/logs/varnish.log &
To view the number of varnish server connections and hit rates:
/usr/local/varnish-2.1.3/bin/varnishstat
The above is the state of varnish,
The number of client requests received by the 1675 0.00 0.06 clients requests received for the server
179 0.00 0.01 Cache hits a hit cache, the number of times the data is returned to the client from the cache, that is, the hit rate
One 0.00 0.00 Cache misses the number of times the data is returned to the user from the backend service application for Skip pass caching
Use Help to see which varnish commands you can use:
/usr/local/varnish-2.1.3/bin/varnishadm-t 127.0.0.1:3500 Help