How to set a custom cache expiry time for a specific page on your website

3 min read

If you’re having trouble with a page that includes a form, sign-up process, optin or whatever it may be due to caching, and excluding it from the cache isn’t an option, then this article is for you.

We see this on pages like the WP Ultimo sign-up page when using a custom URL, and we’ve also had reports of it happening on pages that have forms such as Caldera forms, Convert Pro and Ultimate Addons for Beaver Builder. 

We’ll use the same file name outlined in the exclude a page from server caching article, and if you need to exclude pages from the cache, you can add those here as well. Check out this article for more details on excluding pages from the cache:

https://gridpane.zendesk.com/hc/en-us/articles/360037503471-Exclude-a-page-from-server-caching

This uses the Redis or FastCGI .conf wildcard includes contained in sites Nginx virtual server:

/var/www/site.url/nginx/*-skip-redis-cache.conf
/var/www/site.url/nginx/*-skip-fcgi-cache-context.conf

Step 1: Log into your server as root

Use your favorite SSH client and connect to your server IP as root. If you’re not sure how to do this, please see the following articles to get started:

Generate your SSH Key:

Generate SSH Key on Mac

Generate SSH Key on Windows with Putty

Generate SSH Key on Windows with Windows Subsystem for Linux

Generate SSH Key on Windows with Windows CMD/PowerShell

Add your SSH Key to GridPane:

Add default SSH Keys

Add/Remove an SSH Key to/from an Active GridPane Server

Connect to your server:

Connect to a GridPane server by SSH as Root user.

 

Step 2: Add the cache expiry time for your specific page

If you’re using Redis page caching the default expiry time is 30 days. Run the following command (switching out “site.url” for your websites domain) to create your custom config:

nano /var/www/site.url/nginx/custom-skip-redis-cache-context.conf

Example:

nano /var/www/gridpane.com/nginx/custom-skip-redis-cache-context.conf

If you’re using FastCGI page caching, you shouldn’t experience this problem as the cache refreshes every 1s. However, if you’ve changed the default refresh time you can add your rule with the following (switching out “site.url” for your websites domain):

nano /var/www/site.url/nginx/custom-skip-fcgi-cache-context.conf

Then copy the information below and paste it inside your SSH client. On some machines this will be a right click with your mouse, not a CTRL + V.

if ($request_uri ~* "(/your-page/)") {
 set $cache_ttl 60;
}

The top line should contain your sign up url. For example, if your page was domain.com/your-page, the above would work correctly. You need to switch this out for the page you want to exclude. For multiple pages, it would look like this:

if ($request_uri ~* "(/your-page/|/page-two/|/page-three/ )") {
 set $cache_ttl 60;
}

The “60” means 60 seconds. You need to change this to whatever makes sense for you.

Here’s a handy time converter you can use to convert minutes/hours into seconds:
https://www.calculatorsoup.com/calculators/conversions/time.php

Example

If we wanted the cache on gridpane.com/apollo to expire after 1 hour, we would add the following to our config:

if ($request_uri ~* "(/apollo/)") {
 set $cache_ttl 3600;
}

Step 3: Test your nginx (important)

You’ll want to run an “nginx -t” on the command line.

nginx -t

This will test your configs, and if all is well, it will show the below message. If it shows any failures, or any message other than what is below, do NOT proceed to step 4:

root@servername:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@servername:~#

Step 4: Reload nginx configuration

Remember, if you didn’t have a successful test above, do not proceed. Finally, reload your nginx config by running:

gp ngx reload

You’re now all set 🙂

The image below shows a custom expiry time of 30 seconds: