nginx and cache plugins

Undoubtedly, nginx is one of the best web servers available today, but its configuration has to be done through a configuration file that, as a general rule, can not be modified on the fly.

This means that when a cache plugin is installed the changes cannot be applied as in Apache HTTPD or others through the .htaccess file, so the plugin cannot make the corresponding changes to take advantage of the cache.

When activating the plugin, it will begin to store the files in the corresponding folders, so we have to make nginx call that cache before executing the normal commands.

There is a very simple caching plugin called Simple Cache. This plugin manages the cache and its changes and creates a URL structure directly on the file system. In this way the cache is in an address similar to this:


In this case, the usual WordPress cache folder is used, followed by the plugin name and then the URL structure.

nginx by default executes the following call:

location / {
  try_files $uri $uri/ /index.php?$args;

This means that it analyzes the URLs, checks if the file really exists (and if it exists it returns it) and, otherwise, always ends up called to index.php. This operation is the same as the code that by default is added to the .htaccess.

To make you try to test the cache call first to see if it already exists, we have a simple formula, adding these two previous elements:

location / {
 try_files "/wp-content/cache/simple-cache/${http_host}{request_uri}index.html" $uri $uri/ /index.php?$args;

This system would only work in the case where the permalinks are configured as follows:


In the initial part you can configure whatever you want, but the last parameter must be the %postname% final bar finish.

In case the permalinks do not have the final bar, the following code should be used:

location / {
 try_files "/wp-content/cache/simple-cache/${http_host}{request_uri}/index.html" $uri $uri/ /index.php?$args;

If the plugin, as is the case, allows you to activate the Gzip option, you must deactivate it since nginx already has this system activated by default, so the data will be saved plans and nginx will already execute the best option.

About this document

This document is regulated by the EUPL v1.2 license, published in WP SysAdmin and created by Javier Casares. Please, if you use this content in your website, your presentation or any material you distribute, remember to mention this site or its author, and having to put the material you create under EUPL license.