fastcgi_cache with conditional purging
Check if your nginx has fastcgi_cache_purge module
Support for fastcgi_cache_purge
should be already there. You can test it by running following command:
1nginx -V 2>&1 | grep nginx-cache-purge -o
If you see nginx-cache-purge
in output then you already have it.
Otherwise, if you are on Ubuntu with default Nginx installation, you can run following commands to install nginx with fastcgi_cache_purge
module.
Reinstall nginx with fastcgi_cache purge module support
1sudo add-apt-repository ppa:rtcamp/nginx
2sudo apt-get update
3sudo apt-get remove nginx*
4sudo apt-get install nginx-custom
Install Nginx Helper Plugin
Above step ensures that Nginx can purge a page from its fastcgi_cache
selectively. But Nginx cannot automatically find out which page to purge and when to purge?
So install Nginx helper plugin from WordPress plugin repository and activate it. Apart from other features, it provides cache purging options. Just activate it, go to its settings and turn on “Enable Cache Purge” option.
If you want more control over your cache purging rules, you can play with different purging options it provides.
Using ramdisk (tmpfs) for cached content
This step is optional. You need give Nginx a folder store fastcgi_cache
content. I will recommend using /var/run
on Ubuntu as its mounted as tmpfs (in RAM). If you do not have ample RAM you can pick any other location.
If you are going with RAM, make sure you check size of /var/run
folder. Its generally 20% of your total RAM size.
To verify it, run command df -h /var/run
. You will see output like below:
1Filesystem Size Used Avail Use% Mounted on
2tmpfs 6.3G 364K 6.3G 1% /run
It looks like we have more than 6GB at our disposal! Our server has 32GB RAM by the way, so 6.3GB is close to 20%
Nginx Config
No matter how you are using WordPress, i.e. single or Multisite (with subdirectory/subdomain/domain-mapping) fastcgi_cache
related configuration will remain similar.
Now make changes to /etc/nginx/sites-available/example.com
file so it looks like one below:
1# move next 4 lines to /etc/nginx/nginx.conf if you want to use fastcgi_cache across many sites
2fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
3fastcgi_cache_key "$scheme$request_method$host$request_uri";
4fastcgi_cache_use_stale error timeout invalid_header http_500;
5fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
6server {
7 server_name example.com www.example.com;
8
9 access_log /var/log/nginx/example.com.access.log;
10 error_log /var/log/nginx/example.com.error.log;
11
12 root /var/www/example.com/htdocs;
13 index index.php;
14
15 set $skip_cache 0;
16
17 # POST requests and urls with a query string should always go to PHP
18 if ($request_method = POST) {
19 set $skip_cache 1;
20 }
21 if ($query_string != "") {
22 set $skip_cache 1;
23 }
24
25 # Don't cache uris containing the following segments
26 if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
27 set $skip_cache 1;
28 }
29
30 # Don't use the cache for logged in users or recent commenters
31 if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
32 set $skip_cache 1;
33 }
34
35 location / {
36 try_files $uri $uri/ /index.php?$args;
37 }
38
39 location ~ \.php$ {
40 try_files $uri =404;
41 include fastcgi_params;
42 fastcgi_pass 127.0.0.1:9000;
43
44 fastcgi_cache_bypass $skip_cache;
45 fastcgi_no_cache $skip_cache;
46
47 fastcgi_cache WORDPRESS;
48 fastcgi_cache_valid 60m;
49 }
50
51 location ~ /purge(/.*) {
52 fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
53 }
54
55 location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
56 access_log off; log_not_found off; expires max;
57 }
58
59 location = /robots.txt { access_log off; log_not_found off; }
60 location ~ /\. { deny all; access_log off; log_not_found off; }
61}
The line fastcgi_cache_use_stale
is what makes caching on Nginx-side unique. This line tells Nginx to use old (stale) cached version of page if PHP crashes. This is something not possible with WordPress caching plugins.
Don’t Forget
Always test your Nginx configuration and then reload it. All changes to Nginx config must be followed with these commands:
1nginx -t && service nginx reload
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/fastcgi-cache-purge/6869.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.