Varnish For Wordpress

My computer develop environment

1
2
$ cat /etc/system-release
CentOS Linux release 7.7.1908 (Core)

Recently, in the cache test part, although W3 Total Cache is great, but the network request must go through PHP to get the records in the database, and it is limited by the database and hard disk.
There are some plugins that can achieve pseudo-staticization and improve a lot of speed.
But there are still many things that are limited.
Later, I wanted to say that I use Varnish, save it in php, and put the cache in memory.
So here is this record.

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
vcl 4.0;

backend default {
.host = "127.0.0.1";
.port = "8080";
}

acl purge {
"localhost";
"127.0.0.1";
}

sub vcl_recv {

if (req.method == "PURGE") {
if (!client.ip ~ purge) {
return(synth(405, "Deny"));
}
return (purge);
}

if (req.http.Authorization || req.method == "POST") {
return (pass);
}
if (req.url ~ "(wp-login|wp-admin|feed)" || req.url ~ "preview=true" || req.url ~ "(xmlrpc.php|wp-cron.php)") {
return (pass);
}
if (req.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
unset req.http.cookie;
}

if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
# return (pass);
}

return (hash);
}

sub vcl_pipe {
return (pipe);
}

sub vcl_pass {
return (fetch);
}

sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
}

sub vcl_backend_response {

if (bereq.url ~ "\.(css|js|png|gif|jp(e?)g)|swf|ico") {
unset beresp.http.cookie;
}

if (beresp.http.Set-Cookie && bereq.url !~ "^/wp-(login|admin)") {
unset beresp.http.Set-Cookie;
}

if ( bereq.method == "POST" || bereq.http.Authorization ) {
set beresp.uncacheable = true;
set beresp.ttl = 120s;
return (deliver);
}

if ( bereq.url ~ "\?s=" ){
set beresp.uncacheable = true;
set beresp.ttl = 120s;
return (deliver);
}

if ( beresp.status != 200 ) {
set beresp.uncacheable = true;
set beresp.ttl = 120s;
return (deliver);
}

set beresp.ttl = 72h;
set beresp.grace = 30s;

return (deliver);
}

sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-cache = "HIT";
} else {
set resp.http.X-cache = "Miss";
}
unset resp.http.Via;
unset resp.http.X-Varnish;
set resp.http.X-Cache-Hits = obj.hits;
return (deliver);
}

sub vcl_init {
return (ok);
}

sub vcl_fini {
return (ok);
}

Thank you for reading.
This post is copyrighted by Liyuliang’s Blog.
If reproduced, please indicate the source: Liyuliang’s Blog
This blog uses Creative Commons Attribution-NonCommercial-Share-Sharing 4.0 International License Agreement to license.


Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×