How do you setup full page caching?

outbound

Verified User
Joined
Jun 20, 2023
Messages
5
Hi there,

I'm on a Managed VPS with AlmaLinux 8, Nginx and PHP-FPM (PHP 8.1x) running mostly Wordpress sites.

Having setup Nginx and PHP with Fast-CGI, I want to now set up page caching on my Wordpress pages, as it doesn't seem like that's happening right now. I can verify that FastCGI has been configured and is running.

I've done a fair bit of research, but some of the articles are as far back as 2011, so not sure what to trust.

QUESTION:
Is this something that's accomplished using directives that go in a configuration file, or do I use a plugin like W3-Total Cache - or both?

Sorry, if I appear to be a bit lost. I'm technically inclined, but not a developer. I will get technical resources to help me, once I know what to do.

Thanks in advance.
 
Hi there,

I'm on a Managed VPS with AlmaLinux 8, Nginx and PHP-FPM (PHP 8.1x) running mostly Wordpress sites.

Having setup Nginx and PHP with Fast-CGI, I want to now set up page caching on my Wordpress pages, as it doesn't seem like that's happening right now. I can verify that FastCGI has been configured and is running.

I've done a fair bit of research, but some of the articles are as far back as 2011, so not sure what to trust.

QUESTION:
Is this something that's accomplished using directives that go in a configuration file, or do I use a plugin like W3-Total Cache - or both?

Sorry, if I appear to be a bit lost. I'm technically inclined, but not a developer. I will get technical resources to help me, once I know what to do.

Thanks in advance.
Hey outbound. There are a couple types of caching you're dealing with when setting up a website. There's object caching, which is done at the server level, & then there's page caching, which tends to be done more w/plugins. That's oversimplifying a bit, but it works for purposes of this discussion, I think.

Page caching is great for sites w/a lot of static content--not so much for those w/a lot of dynamic content like forums or a lot of comments occurring simultaneously. Know your sites' content & study how to implement page caching.

The other thing I'd say about page caching is that I'm a WordPress support forum contributor & have been for a number of years, & I can't tell you how many posts I've seen where people posted that their website changes weren't being saved & they were pretty much pulling their hair out & blaming their theme or a newly installed plugin. Caching should be the icing on the cake, implemented only after the vast majority of site development is complete.
 
Thanks so much, and totally get it.
Thanks for helping me understand the lingo, and differentiating the two types of caching.

My site will mostly be static content, that outside of the blog will not change frequently.

A key part of the reason I went to Nginx was to leverage server-side caching. I have used plugins like WP-Rocket and Perfmatters, so will do what I can in the Wordpress side of things, by using W3 Total Cache which seems to be quite Nginx friendly.

Just trying to get to server-side caching (and then purging) so I don't have to use Lightspeed if I don't need to.

So any help with server-side caching would be appreciated.

An example of an article I found was this - https://www.linode.com/docs/guides/how-to-use-nginx-fastcgi-page-cache-with-wordpress/ - which doesn't rely on plugins to set it up.
Then there is this - https://wpastra.com/guides-and-tutorials/wordpress-object-caching/ - which seems to go a plugin route.

Hence my confusion on how to go about it. ;)

Cheers.
 
Last edited:
Thanks so much, and totally get it.
Thanks for helping me understand the lingo, and differentiating the two types of caching.

My site will mostly be static content, that outside of the blog will not change frequently.

A key part of the reason I went to Nginx was to leverage server-side caching. I have used plugins like WP-Rocket and Perfmatters, so will do what I can in the Wordpress side of things, by using W3 Total Cache which seems to be quite Nginx friendly.

Just trying to get to server-side caching (and then purging) so I don't have to use Lightspeed if I don't need to.

So any help with server-side caching would be appreciated.

An example of an article I found was this - https://www.linode.com/docs/guides/how-to-use-nginx-fastcgi-page-cache-with-wordpress/ - which doesn't rely on plugins to set it up.
Then there is this - https://wpastra.com/guides-and-tutorials/wordpress-object-caching/ - which seems to go a plugin route.

Hence my confusion on how to go about it. ;)

Cheers.
It is included in pro pack. https://docs.directadmin.com/gettin...ml#nginx-fastcgi-cache-for-wordpress-pro-pack
 
Thank you. I took a look at the DA Propack template, the code added by it to the nginx_php.conf file (which was a tad different!) and also the well documented linode example. Based on that I've come up with something that I need some help with.

PHP:
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=wpcache:200m max_size=1g inactive=2h use_temp_path=off;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

#INFO=name=WordPress (FastCGI Cache)
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 ~* "${template_location}wp-admin/|${template_location}xmlrpc.php|${template_location}wp-.*.php|${template_location}feed/|${template_location}index.php|${template_location}sitemap(_index)?.xml") {
    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;
}

location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        include snippets/fastcgi-php.conf;
        fastcgi_cache wpcache;
        fastcgi_cache_valid 200 301 302 2h;
        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;
    }

    location ~ /\.ht {
        deny all;
    }

location ~ /purge(/.*) {
    fastcgi_cache_purge FASTCGICACHE "$scheme$request_method$host$1";
}

# Wordpress Permalinks
location / {
try_files $uri $uri/ /index.php?$args;
}

QUESTIONS
1. Request filename code


I see this in the ProPack template, but not in the instance.
PHP:
if (!-e $request_filename)
{
    rewrite ^(.+)$ ${template_location}index.php?q=$1 last;
}

And I see this in the user account php conf (traveler is account name)
PHP:
]
if (-f $request_filename)
            {
                fastcgi_pass unix:/usr/local/php81/sockets/traveler.sock;
            }
I assume I need this second one because it references the account name. Do I need the first one - the rewrite command? ANd what purpose does that serve?

2. Location items - sequencing, redundancy
From reading around, I've learned that sequencing is important. Is the full code above sequenced correctly? Especially the different location statements.

3. Location Deny item
Is this needed if I'm not running Apache? Is there an alternative for nginx + PHP-FPM?


PHP:
 location ~ /\.ht {
        deny all;

Many thanks!
 
Why do not you just make the selection in GUI for the domain you need it?
 
Hi there,
I did - but I was getting a cache MISS. Which is what started me on this journey.

With the file above I get the following error


Code:
nginx: [emerg] "fastcgi_cache_path" directive is not allowed here in /usr/local/directadmin/data/users/traveler/nginx_php.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
 
Cache hits aren't related to information printed in headers :) I mean cache can be hit without headers showing any information.

./configure/nginx/conf/nginx-fastcgi-cache.conf does not add any header to the responses, however, you may see cache files created in /var/run/nginx-fastcgi-cache when cache is activated.

There shouldn't be any need to do configure anything manually when fastcgi cache for wordpress has been chosen as template in user level.
 
Got it. The reason I'm trying to expand on the standard file is three fold.

1. Exclude Wordpress admin and sitemap pages

PHP:
# Don't cache uris containing the following segments
if ($request_uri ~* "${template_location}wp-admin/|${template_location}xmlrpc.php|${template_location}wp-.*.php|${template_location}feed/|${template_location}index.php|${template_location}sitemap(_index)?.xml") {
    set $skip_cache 1;
}

2. Exclude logged in Wordpress users

Code:
# 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;
}

2. Set a different duration for certain file types, errors
- shorter for 302
- max expiration for docs
- lower expiration for .mp4 files.

Code:
location ~* ^.+.(ogg|ogv|svg|svgz|eot|otf|woff|ttf|css|rss|atom|js|jpg|jpeg
|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi
|wav|bmp|rtf)$ {

expires max;
}

Can I simply add these to the standard file?

Thanks for your help.

It's much appreciated.
 
Last edited:
Back
Top