Recommended VPS Complete list of best VPS hosting providers.

Setup Nginx Virtual Host For WordPress With W3 Total Cache Plugin

Nginx has no .htaccess file! Similar configuration can be done via its virtual hosts file. I explained step by step tutorial how to setup a working Ubuntu server with LNMP (Linux, Nginx, MySQL, PHP) stack so you can host your WordPress-based blog in it. I also told you a standard config to put in your virtual hosts file so you can use WordPress pretty / SEO-friendly permalink. Today, I once again will share a secret sauce on how to use world’s most popular caching plugin for WordPress, the W3 Total Cache (w3tc).

Using any caching plugin is aimed to gain more performance in term of page load speed as well as to decrease server load at once. W3TC is one of the best caching plugins available freely. However, if you decide to install and use this plugin on your blog hosted in a server running Nginx, then you should also perform additional task: to add certain configuration in your Nginx virtual hosts file to reflect the same function as what in Apache rewrite rules (in .htaccess file). Anyway, Nginx virtual hosts file is not the same as Apache .htaccess file.

Prerequisite

A working server running Nginx web server. In this example I use Ubuntu. I wrote several articles you can follow to build the same Ubuntu server with LNMP stack. Read:
Basic Ubuntu setup
How to install Nginx and PHP5 on Ubuntu
How to install MySQL server on Ubuntu
How to install Postfix on Ubuntu
How to install WordPress on Ubuntu server running Nginx

Assumptions: By reading this guide I assume you have enough basic knowledge about WordPress, about how to SSH-ing to your server, and also have a server with Nginx on it.

How to configure Nginx Configuration For W3 Total Cache Plugin

Step 1 – Login to your server via SSH client you like. In this case I use Putty because I’m a Windows user. Once logged in, you may better login with root privilege. In Ubuntu you can simply type “su” command then hit Enter.

login ssh

Step 2 – Now it is time to edit the virtual hosts file for your blog’s domain name. Use following command:

nano /etc/nginx/sites-available/domain.com

p.s: please remember to change / replace “domain.com” with your own. You may also replace “htdocs” with “public_html” if you use it as your web root directory but in this example I use “htdocs”.

edit nginx virtual hosts file

Step 3 – If you are still using default one as mentioned in my previous guide, you can simply clear them all and replace with this one below..

server {
	server_name domain.com www.domain.com;

	access_log   /var/log/nginx/domain.com.access.log;
	error_log    /var/log/nginx/domain.com.error.log debug;

	root /var/www/domain.com/htdocs;
	index index.php;

	set $cache_uri $request_uri;

	# POST requests and urls with a query string should always go to PHP
	if ($request_method = POST) {
		set $cache_uri 'null cache';
	}   
	if ($query_string != "") {
		set $cache_uri 'null cache';
	}   

	# Don't cache uris containing the following segments
	if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
		set $cache_uri 'null cache';
	}   

	# Don't use the cache for logged in users or recent commenters
	if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
		set $cache_uri 'null cache';
	}

	# Use cached or actual file if they exists, otherwise pass request to WordPress
	location / {
		try_files /wp-content/w3tc/pgcache/$cache_uri/_index.html $uri $uri/ /index.php?$args ;
	}    

	location = /favicon.ico { log_not_found off; access_log off; }
	location = /robots.txt  { log_not_found off; access_log off; }

	location ~ .php$ {
		try_files $uri /index.php; 
		include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
	}

	# Cache static files for as long as possible
	location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
               expires max; log_not_found off; access_log off;
	}
}

Once done, now hit Control+O to save then Control+X to exit Nano editor. Here’s what it may be seen like..

w3tc rewrite rules on nginx

Step 4 – Now test that new configuration and restart Nginx service as well. This command may help you..

nginx -t && service nginx restart

nginx test and restart

Step 5 – Then login to your WordPress Admin Dashboard. Once you logged in, go to W3TC General Settings page.

w3tc general setting

Step 6 – First, go to the Page Cache section. Enable that option and choose “Disk Enhanced” from the drop down menu.

w3tc page cache

Step 7 – Now move down a little bit. In the Minify section, make sure it is disabled. Why? Personally I think not all other plugin and themes support this feature. However if you are really sure that the theme you are using can be minified nicely, you can enable it as you wish.

disable w3tc minify

Step 8 – Then move one to the next section which is Database Cache. Enable this option and choose “Opcode: Alternative PHP Cache (APC)“.

w3tc database cache

Upon saving, W3 Total cache will offer APC storage option for Page Cache and Minify Cache also. Do NOT use it. “Disk enhanced” is better for Page Cache & Minify Cache.

Step 9 – Do the same with W3TC’s Obejct Cache option:

w3tc object cache

Step 10 – Next, it is Browser Cache section. You can simply disable this option because Nginx configuration takes care of browser caching at nginx-level very well. It sets cache duration for 25-years already. There is nothing wrong in “Browse Cache” option provided by W3TC plugin. It’s just that nginx can handle it faster and itself.

w3tc browser cache

Step 11 – Next section is CDN (Content Delivery Network). You can either enable or disable it as you wish. But make sure you are full understand how to use it. It requires no configuration at Nginx level.

Step 12 – I simply disable Reserve Proxy option as here I don’t use Varnish Cache in my server. What about you?

Step 13 – The rest options are your choice. Once done, make sure to hit the “Save All Settings” button.

Thanks to: RtCamp.

Hmmm,.. I hope your blog can run faster!! The plugin is claimed as The only WordPress Performance Optimization (WPO) framework; designed to improve user experience and page speed. Personally I also prefer to use W3 Total Cache than any other caching plugins available out there.

3 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.