ProgrammingTutorials

“WP Fastest Cache” Configuration for Nginx

Wordpress Nginx WP Fastest Cache Configuration

Wordpress Nginx WP Fastest Cache Configuration

The tutorial was updated to reflect the new changes (2017/01/27 GMT +05.30)

Scope

This article only covers the configuration part of Nginx. If WP Fastest Cache isn’t already installed, and if you are completely new to either Nginx or WP Fastest Cache, then DO NOT read beyond at this point, as it’s not worth the time. This article is for those who have a working Nginx web server, a working wordpress site where WP Fastest Cache is installed, but not configured to work with Nginx.

Introduction

I have been using many cache plugins for wordpress ever since I started using wordpress platform for writing blog articles, but I didn’t like anything so far except WP Fastest Cache. Unlike the other cache plugins, WP Fastest Cache has been working fine from the beginning smoothly without any problems and without bloating its source codes. Even though it has less features compared to W3 Total Cache, WP Total Cache, it is sufficient for me, and is extremely lightweight for the server. This whole things made me to stick with WP Fastest Cache from the beginning, and didn’t make my mind to leave it and find another even when I changed the web server to Nginx.

The plugin stated in its FAQ section, it supports to Nginx, but it turned out it only supports when Nginx is setup as a proxy server for Apache, where Nginx works in the front-end, meanwhile the apache does all the heavy dynamic content processing while sitting in the back-end. I contacted author and asked from him how to make this plugin works with Nginx, as you might have guessed, he stated the same thing what I also found through another reply as stated earlier. This almost made me to give up on WP Fastest Cache, and look elsewhere, but I couldn’t leave it due to its uniqueness, and functionality. Even though while I had been searching, I could find a working code, it wasn’t enough for me as it  doesn’t cover certain conditions. so I decided to write my own code, and test it in my own server to see how it turns out. With a load of trials and errors, finally I could produce something working, optimized, and almost equal to its apache counterpart.

Code Snippet

Copy and paste the following code snippet in the server { } block of “default” file (or whatever the file is being used) located in /etc/nginx/sites-enabled/. Make sure to remove the existing location / { } block before using the one in the following code snippet.

server {
	listen 80;
	root /var/www/html;
	index index.php index.html index.htm;
	server_name nucuta.com www.nucuta.com;
	
	#CACHE
		set $condition '';
		if ($request_method = POST)
		{
			set $condition "null";
		}
		if ($query_string !~ "")
		{
			set $condition "null";
		}
		if ($http_cookie ~* (comment_author|comment_author_|wordpress_logged_in|wp_woocommerce_session))
		{
			set $condition "null";
		}
		set $fullurl '/wp-content/cache/all${condition}';
	#CACHE ENDING
	
	
	location / {
		gzip_static on; 
		add_header Nucuta-Cache wp_fastest_cache_enabled;
		add_header Nucuta-Cache-Location $fullurl${request_uri}index.html;
		try_files $fullurl${request_uri}index.html $uri $uri/ /index.php$is_args$args;
		
	}

	location ~ \.php$ {
		add_header Nucuta-Cache wp_fastest_cache_disabled;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
	}
}

Explanation of Code Snippet

  • It creates a variable and assigns nothing to its container.
  • It checks for the the request_method, and if it’s POST, it sets the aforesaid variable – condition to null, which is just a string NOT a boolean.
  • In the next block it checks for query string, such as anything that comes after ? in a link (in youtube ?v=_lx9pZbhAhc). !~ implies, it checks for NOT, meaning when there is a query string in the URL, it sets the condition to null.
  • In the next block, it checks the cookies, which are useful to ignore logged in users/admins/people who made comments/if it’s an e-commerce website, then customers, additionally those who use CLEF login plugin. Basically if request header contains one of these cookie names, it sets the condition variable to null, implying the users who send such requests are ignored from serving cache files. ~* operating means, it doesn’t strictly checks cases, meaning it works in case-insensitive ways. Logged off state is also taken into considered, meaning if an user logged out , and starts using the same browser for exploring the site, they are being served the cached contents just as to a guest visitor.
  • In the last block before the “CACHE” comment, It creates another variable and names it $fullurl, and assigns it the path where the cache files are stored, in my server since the cache files are stored in “/var/www/html/wp-content/cache/all/” , that path is used. The next $condition is for triggering an artificial 404 state if the given conditions are met. As an example, if an logged in user visited the site, the $fullurl’s path returns as /var/www/html/wp-content/cache/allnull, since it doesn’t exist, a fresh content is served to that particular user.
  • gzip_static is turned on to check for existence of pre-compressed files. Make sure to enable both gzip on;gzip_vary on; in /etc/nginx/nginx.conf in your server. text/html also known as HTML files are compressed in default without even mentioning the gzip_types directive. Please refer this article for more information.

Mechanism and try_files

The basic logic in here is, if none of the above conditions are met, then $condition variable is nothing, and thus the $fullurl  is simply “/var/www/html/wp-content/cache/all” as it is supposed to be, but if the conditions are met, then it’s /var/www/html/wp-content/cache/allnull. Finally in the location block, try_files directive is used to serve contents depending on their availability. First it tries to serve $firsturl which is declared in the server block, as explained early. If the conditions are met, it comes out as /var/www/html/wp-content/cache/allnull/index.html , leading to throw 404 error, but since try_files directive is used, it passes to the next order which is /index.php$is_args$args, meaning let the wordpress to take care of the request, meaning serve a fresh content.

In $fullurl${request_uri}index.html, when an user requests a content of the root folder, $request_uri returns as “/”, if it to either a post or a page, then it would be “/<post or page name>/” (as an example /how-to-change-your-wifi-password/ in http://nucuta.com/how-to-change-your-wifi-password/) (ignore the double qutations).

Relative to root path /var/www/htmlIf none of the conditions are metIf at least one of the condition is met
try_files$fullurl${request_uri}index.html$fullurl${request_uri}index.html
Request to root/wp-content/cache/all / index.html/wp-content/cache/allnull / index.html
Request to a page/wp-content/cache/all /<page name>/ index.html/wp-content/cache/allnull /<page name>/ index.html
Request to a post/wp-content/cache/all /<post name>/ index.html/wp-content/cache/allnull /<post name>/ index.html

Use $uri if you intend to serve any file format other than html/php, use $uri/ if you intend to serve directories, be aware that if directory listing is enabled, any request to such directory will lead the browser to show all the contents in that particular folder as in windows explorer. It’s strongly recommended to use $uri, as not using it leads to unable to serve and upload static contents like images, text files etc..

You may remove “add_header” headers, as they are used to debug the codes. If you want to see it yourself, then let them be in the code, and examine the header through F12 -> Network in Chrome to make sure the codes are working as they are supposed to be.

Don’t forget to use the respective…

listen 80;
 root /var/www/html;
 index index.php index.html index.htm;
 server_name nucuta.com www.nucuta.com;

pertain to your server, but the root folder should always be somewhere up in the same line as the cache folder. As an example, if the root folder is /var/www/html, the cache folder should be under the html folder, the position is not relevant, the cache folder can be in /var/www/html/cache  or /var/www/html/1/2/3/cache. Make sure to use the index.php before index.html, in index directive.

Tags:

Leave a Reply

Be the First to Comment!

Notify of
avatar
1500
wpDiscuz
nucuta header blue