Site icon Ryadel

Nginx Reverse Proxy Cache for WordPress

Wordpress - How to Fix Home Page Caching Issues by setting the Last-Modified response Header to the most recent post

Some weeks ago we published a tutorial instructing how to use Nginx as a Reverse Proxy with buffering & cache functions using the standard edge-origin pattern. The configuration settings shown within that guide were ok to use for generic web sites, but we can definitely optimize them better for specific caching scenarios. In this follow-up article we'll learn how to fine-tune the nginx.conf configuration file to perform well as a Reverse Proxy with caching and request buffering features for a WordPress-enabled web site.

It goes without saying that the origin server - the one serving the WP web site - can be any Linux or Windows machine with either Apache, Nginx, IIS or other web servers, while the edge server - the one hosting Nginx and actually accepting the user requests - is meant to be a Linux-based environment. The two services could even be installed within the same machine, providing that the one acting as the origin will be configured to answer to a different port (such as 8080): however, configuring everything within a single machine should be avoided for high traffic web sites for obvious performance reasons.

Http

Here's the configuration settings that can be put within the http block of the nginx.conf file:

All the settings are well-explained in the various comments: if you need additional info, you can also look for them within the official Nginx documentation by clicking here.

It's worth noting that, if you don't want to stack up everything in your main configuration file, you can also put this whole part to a new http.reverseproxy.conf file - which you can add in the /etc/nginx/  folder - and then link it by adding the following line within the http block of the nginx.conf file itself:

Server

Here's the configuration settings that can be put within the server block of the nginx.conf file, right below the listen  and server_name  settings:

Location

Last but not least, here's how to configure the location block:

As we can see, we made sure that Nginx cache will be bypassed whenever it needs to (WP auth cookies, wp-admin requests and so on), so that the basic WordPress functions won't be crippled.

Results

Here's how we can put everything together into a typical Nginx configuration file (nginx.conf), assuming that we stacked everything up there:

Troubleshooting

The most common issues we can run into while configuring Nginx as a reverse proxy are related to file permissions: ensure that the nginx user - or any other user set by the user  configuration setting within the nginx.conf file - has the right permissions to access the working_directory , proxy_cache_path  and proxy_temp_path  folders in read/write mode. If this is not enough, remember to check out the /var/log/nginx/access.log  and /var/log/nginx/error.log  files for useful insights regarding your specific issue(s).

Conclusions

And that's it! I sincerely hope that these settings will help those who need to optimize their Nginx configuration for WordPress.

This post is part of a series of articles, tutorials and guides on the NGINX web server & reverse proxy. To read the other posts, click here!

 

Exit mobile version