天天看點

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?

為什麼要使用X-Purge-Regex?

因為vcl中的req.url擷取的是curl的url的path,而這個path是不能傳遞正規表達式的,是以放到header中,自定義個屬性來判斷。

使用這個function