How to setup NGINX fastcgi cache with WordPress

Dennis Andersson – April 10, 2021

Nginx includes a FastCGI module that has directives for caching dynamic content served from WordPress. Setting this removes the need for additional solutions for page cachings such as Varnish or WordPress plugins. Content can also be excluded from caching based on the query method, URL, cookies or any other server variable. For example, you do not want to cache what is behind / wp-admin or if you have a cookie that says you are logged in.

How does NGINX fastcgi cache work?

When someone visits your WordPress site, your server must run PHP and query your database to deliver the final HTML page to your visitors’ browsers. Caching works by storing the final HTML page and automatically delivering it to visitors, instead of dynamically rendering your content for each visit.

By eliminating this backend processing, caching can speed up your site and reduce the load on your server.

With Nginx you can do something similar via its fastcgi_cache module but at the server level. This means that Nginx can serve your cached content directly without having to involve PHP or WordPress.

However, with a cache plugin, every request must still be handled by PHP at the application level. A caching plugin still eliminates a lot of work and is definitely positive for your site, but it also requires more CPU cycles than using Nginx to handle server-level requests. Therefore, NGINX cache is the best option for WordPress and is used by the most modern web hosts for wordpress.

How to enable NGINX fastcgi cache for WordPress

Edit nginx main config

sudo nano /etc/nginx/nginx.conf

In the http section add:

fastcgi_cache_path /usr/share/nginx/fastcgi_cache levels=1:2 keys_zone=WORDPRESS:100m max_size=10g inactive=60m use_temp_path=off;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

Now the cache is activated in nginx but we also need to activate it per site by editing the file under conf.d.

sudo nano /etc/nginx/conf.d/your-hompage.conf

Find the piece that looks like this: location ~ .php$

Add this within the location block

fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
fastcgi_cache_min_uses 1;
fastcgi_cache_lock on;
fastcgi_cache_bypass $skip_cache; 
fastcgi_no_cache $skip_cache;
add_header X-FastCGI-Cache $upstream_cache_status;

Save the file and test the configuration by running:

sudo nginx -t

If everything looks good, we reload the NGINX configuration

sudo service nginx reload eller sudo systemctl reload nginx

Test if the cache works

curl -I http://www.din-hemsida.com

In the answer you should see X-FastCGI-Cache header. HIT if you test twice. The first time the page is uncached and the next it should be cached.

Lägg till cacheregler för WordPress

Place this above the location block with location ~ .php $ in /etc/nginx/conf.d/your-homepage.conf

set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-..php|^/feed/|/tag/./feed/|index.php|/.sitemap..(xml|xsl)") {
set $skip_cache 1;
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}

Velumi and NGINX fastcgi cache

At Velumi, we use NGINX fastcgi cache or nginx redis cache in all our packages pre-configured and optimized with automatic cache clearing when you update a page in WordPress. Contact us in the chat if you have any questions or if you want to know more.

Want to know more?


Similar articles