Exclude a page from server caching

3 min read

If you’re building projects for your clients, or you’re managing a website that has some complex functionality, you may find that you need a page to be excluded from the cache for it to work correctly.

This article will walk you through how to set this up with GridPane’s server caching options.

Before we begin, be sure to confirm which cache type you’re using inside of your websites customizer by heading to you Sites page inside your GridPane account and clicking on your sites domain name.

Step 1: Log into server as root

Use your favorite SSH client and connect to your server IP as root. If this is your first time connecting to one of your servers, please see the following articles to get started:

Step 2: Exclude your page/s

Now inside your server we need to add our page exclusions. Be sure only to use correct command below for the type of page caching you’re using. Adding our exclusion to the wrong caching method will have no effect.

Redis: If you’re using Redis page caching, enter the following and hit enter (replacing site.url with your domain name):

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

FastCGI: If you’re using FastCGI page caching,enter the following and hit enter (replacing site.url with your domain name):

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

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

if ($request_uri ~* "(/examplepage/)") {
 set $skip_cache 1;
 set $skip_reason "${skip_reason}-request_uri";
}

If all worked correct your screen should look like this:

mceclip0.png

Now just hit CTRL + X on your keyboard, and it will ask if you want to save, click Y to save.

Exclude Multiple Pages

You can exclude multiple pages from the cache like this:

if ($request_uri ~* "(/examplepage/|/another-page/|third-page/)") {
 set $skip_cache 1;
 set $skip_reason "${skip_reason}-request_uri";
}

Wildcard Exclusions

If you want to exclude a parent page plus all child pages, or an entire CPT (or whatever your use case), you can create a wildcard exclude like this:

if ($request_uri ~* "(/parent-page.*)") {
 set $skip_cache 1;
 set $skip_reason "${skip_reason}-request_uri";
}

Exclude with Cookies

This is more advanced, but useful for developers who have specific use cases where adding each page individually / having to update this regularly could be better handled by setting cookies on specific pages instead.

Follow the same steps as above, but instead of adding a page specific exclusion, use the following code block to add a cookie exclusion:

if ($http_cookie ~* "your_cookie_name") {
set $skip_cache 1;
set $skip_reason "${skip_reason}-your_cookie";
}

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:

mceclip1.png

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