Analysis of configuration optimization of varnish cache under Linux _php tutorial

Source: Internet
Author: User
Tags varnish pkill
Varnish is a high-performance open-source HTTP accelerator, and Norway's largest online newspaper, Verdens Gang, used 3 varnish instead of the original 12 squid for better performance than before.

But compared with the old squid, each has its advantages and disadvantages, a large number of online comparison is only in its personal familiar with the application of the maximum use of the play, maybe squid to the ability of the hands to play the most powerful power
Varnish uses the "Visual page Cache" technology, in memory utilization, 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 quickly and bulk clear part of the cache, which squid does not have.
I would like to varnish some ideas and configuration methods to do a simple introduction and notes

Lab environment: Red Hat Enterprise Linux Server Release 5.4 (Tikanga)
Kernel 2.6.18-164.el5
Yum Install Pcre-devel # #预先安装一个软件包, otherwise the error will be prompted
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, determine pass or lookup local query based on VCL
#lookup, find the data in the hash table, enter the hit state if found, otherwise enter the fetch state
#pass, select Background, go to fetch state
#fetch, get the backend for the request, send the request, get the data, and store it locally
#deliver, send the data to the client and go to the done
#done, end of processing
######### #配置后端服务器 ##############
Copy CodeThe code is 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;
}
}

############# #配置后端服务器组, health detection 6 seconds, using the random way to set weights ########
######## #另一种方式round-robin The default polling mechanism ####################
Copy CodeThe code is 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 CodeThe code is as follows:
ACL Local {
"LocalHost";
"127.0.0.1";
}

####### #从url判断针对哪类后面服务器及缓存配置 ############################
Copy CodeThe code is 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, you 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 | Head requests are forwarded directly to the back-end server
if (req.request! = "GET" && req.request! = "HEAD")
{return (pass);}
# #对GET请求, and in the URL with. PHP and. PHP, the end of the, directly forwarded to the back-end server
if (req.request = = "GET" && req.url ~ "\. ( PHP) ($|\?) ")
{return (pass);}
# #对请求中有验证及cookie, forwarded directly to the backend server
if (req.http.Authorization | | req.http.Cookie)
{return (pass);}
{
# #除以上的访问请求, looking from the cache
return (lookup);
}
# #指定的font目录不进行缓存
if (req.url ~ "^/fonts/")
{return (pass);}
}
Sub Vcl_pipe
{return (pipe);}
# #进入pass模式, the request is sent back 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 the following keywords
Sub Vcl_hit
{
if (!obj.cacheable)
{return (pass);}
return (deliver);
}
# #lookup后没有找到缓存时调用, end with the following key words, and call the fetch parameter to re-test whether to join the cache
Sub Vcl_miss
{return (fetch);}
#让varnish服务器缓存的类型, called after data is obtained from the backend
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 formatted files are cached for 7 days and s 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
Create a cache location for Varnish_cache
Mkdir/data/varnish_cache
Start varnish
Ulimit-shn 8192 # # # #设置文件描述符, because my machine performance is not good, 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 to run-G with what group run-F varnish profile-a bind IP and port-S varnish cache file location with size-W minimum, maximum thread and timeout time-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 scripts (/var/logs/cutlog.sh) from last month's log:
vim/usr/local/varnish-2.1.3/etc/varnish/cut_varnish_log.sh
Write the following script:
#!/bin/sh
# This file is 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
Scheduled tasks:
Crontab-e
XX * * * */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

Batch purge cache with regular expressions using the varnish management port
Clear all Caches
/usr/local/varnish-2.1.3/bin/varnishadm-t 127.0.0.1:3500 Url.purge *$
Clears all caches in the 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 CodeThe code is as follows:
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 in the last 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 ratios:
/usr/local/varnish-2.1.3/bin/varnishstat
The above is the state of varnish,
1675 0.00 0.06 client requests received the number of requests received for the server
179 0.00 0.01 Cache Hits is the hit cache, the number of times the data is returned to the client from the cache, that is, the hit rate
0.00 0.00 Cache Misses the number of times the data is returned to the user from the backend service application for skipping the pass cache
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

http://www.bkjia.com/PHPjc/327719.html www.bkjia.com true http://www.bkjia.com/PHPjc/327719.html techarticle Varnish is a high-performance open-source HTTP accelerator, and Norway's largest online newspaper, Verdens Gang, used 3 varnish instead of the original 12 squid for better performance than before. But with the veteran sq ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.