varnish的使用和PHP清除缓存的技巧

官網地址
https://www.varnish-cache.org
安裝路徑
On Debian/Ubuntu this is /etc/default/varnish

設置Backend servers
/etc/varnish/default.vcl

vcl 4.0;

import directors;

# Default backend definition. Set this to point to your content server.
backend default{
.host = "192.168.2.3";
.port = "80";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
.max_connections = 800;
}

admin console
varnishadm

varnish是緩存在內存的,查看log
varnishlog
varnishlog -g raw
varnishtop -i BereqURL # will show you what your backend is being asked the most.
varnishtop -i ReqURL #will show you what URLs are being asked for by the client
varnishtop -I ReqHeader #Accept-Encoding will show the most popular Accept-Encoding header the client are sending you.

varnishstat #統計


查看版本
varnishd -V

停止
service varnish stop

重启
service varnish restart

特别注意:如果修改了vcl文件,必须重启才会生效

Purging and banning
https://www.varnish-cache.org/docs/4.1/users-guide/purging.html
在default.vcl配置文件中設置允許執行purging操作的ip段,下面表示本機和2.0~2.24的IP都可以執行
# Only allow purging from specific IPs
acl purge {
"localhost";
"127.0.0.1";
"192.168.2.0"/24;
}

# This function is used when a request is send by a HTTP client (Browser)
sub vcl_recv {
call detect_device;
# Normalize the header, remove the port (in case you're testing this on various TCP ports)
# set req.http.Host = regsub(req.http.host, ":[0-9]+", "");
# if (req.http.Host == "*.example.com") {
#set req.backend_hint = test.backend();
# }


# Allow purging from ACL
if (req.method == "PURGE") {
# If not allowed then a error 405 is returned
if (!client.ip ~ purge) {
return(synth(405, "This IP is not allowed to send PURGE requests."));
}

#ban("req.http.host == " + req.http.host + " && req.url == " + req.url);
ban("req.http.host == " + req.http.host + " && req.url ~ " + req.url);

# Throw a synthetic page so the
# request won't go to the backend.
return(synth(200, "Ban added"));
}

# Post requests will not be cached
if (req.http.Authorization || req.method == "POST") {
return (pass);
}

# --- Wordpress specific configuration

# Did not cache the RSS feed
if (req.url ~ "/feed") {
return (pass);
}

# Blitz hack
if (req.url ~ "/mu-.*") {
return (pass);
}


# Did not cache the admin and login pages
if (req.url ~ "/wp-(login|admin)") {
return (pass);
}

# Do not cache the WooCommerce pages
### REMOVE IT IF YOU DO NOT USE WOOCOMMERCE ###
#if (req.url ~ "/(cart|my-account|checkout|addons|/?add-to-cart=)") {
# return (pass);
#}

# Remove the "has_js" cookie
set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");

# Remove any Google Analytics based cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");

# Remove the Quant Capital cookies (added by some plugin, all __qca)
set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");

# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");

# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");

# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");

# Are there cookies left with only spaces or that are empty?
if (req.http.cookie ~ "^ *$") {
unset req.http.cookie;
}

# Cache the following files extensions
if (req.url ~ "\.(css|js|png|gif|jp(e)?g|swf|ico)") {
unset req.http.cookie;
}

# Normalize Accept-Encoding header and compression
# https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
if (req.http.Accept-Encoding) {
# Do no compress compressed files...
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
unset req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
unset req.http.Accept-Encoding;
}
}

# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
return (pass);
}
if (!req.http.cookie) {
unset req.http.cookie;
}

# --- End of Wordpress specific configuration

# Did not cache HTTP authentication and HTTP Cookie
#if (req.http.Authorization || req.http.Cookie) {
if (req.http.Authorization) {
# Not cacheable by default
return (pass);
}

# Cache all others requests
return (hash);
}


怎麼判別頁面是varnish生成的?
window在host中添加varnish ip 匹配网站domain,例如
192.168.xx.xx xxx.com

打開website,在console中選擇netword,如果請求項的header中有age說明用了varnish
可以設置多個hos,可以用ip也可以用domain。如果varnish重启了或者purge了缓存,则age会变成0

backend default {
.host = "127.0.0.1";
.port = "8080";
}
We add a new backend.:

backend java {
.host = "127.0.0.1";
.port = "8000";
}

backend server1 {
.host = "server1.example.com";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 5s;
.window = 5;
.threshold = 3;
}
}

backend server2 {
.host = "server2.example.com";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 5s;
.window = 5;
.threshold = 3;
}
}

sub vcl_init {
new bar = directors.round_robin();
bar.add_backend(server1);
bar.add_backend(server2);
}

--------------
vcl 的正則表達式的寫法
https://gist.github.com/dimsemenov/10100415
http://book.varnish-software.com/3.0/VCL_Basics.html
http://www.hostingadvice.com/how-to/varnish-regex/

-----------------
怎么用PHP删除varnish 指定host或某个url的cache?

/**
* @param url string e.g:/Vip/detail?id=99
* @param path string e.g:/Vip/detail
* @param params array,e.g:{id:99}
*/
public function purgeCache($url = "", $path = "", $params = "") {
$varnishHost = C("VARNISH_SERVER");
$regxUrl = "";
if ($url) {
$varnishHost. = $url;
}
elseif($path) {
$regxUrl = "^".$path;
if (!empty($params)) {
$regxUrl. = "(/?)(.*)?";
$regxParams = array();
foreach($params as $key = > $value) {
$regxParams[] = "($key=$value|/$key/$value)";
}
$regxUrl. = implode("", $regxParams);
}
$regxUrl. = ".*$";
}
//$url = "^/Vip/detail(/?)(\?id=99|/id/99)$";
//^/Finance/detail(/?)(.*)?(id=4178|/id/4178).*$
$method = "PURGE";
$hosts = C("VARNISH_HOSTS");
foreach($hosts as $key = > $frontHost) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_URL, $varnishHost);
$headers = array();
$headers[] = 'Connection: Keep-Alive';
$headers[] = "Host: ".$frontHost;
if ($regxUrl) {
$headers[] = "X-Purge-Regex: $regxUrl";
echo "use regex<br>";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, $frontHost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
//dump($data);
$curlErrorCode = curl_errno($ch);
if ($curlErrorCode) {
//echo "purge varnish cache failed.Curl error: " . curl_error($ch);
Log::write("purge varnish cache failed.Curl error: ".curl_error($ch));
}
curl_close($ch);
}

return true;
}



为什么要使用X-Purge-Regex?
因为vcl中的req.url获取的是curl的url的path,而这个path是不能传递正则表达式的,因此放到header中,自定义个属性来判断。

使用这个function


public function purgeTest() {
$url = "";
$path = "/Article/detail";
$params = array("id"=>4178);
$this->purgeCache($url, $path, $params);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值