How to Setup Nginx Virtual Host for WordPress with W3 Total Cache Plugin
Edit Article

  • 1 Editor

There is no .htaccess file in Nginx. We can do the similar configuration through its virtual hosts file. So today, I am going to show you how to use one of the most popular caching plugin for WordPress. This plugin is W3 Total Cache (w3tc).

The main purpose of using any kind of caching plugin is to increase the performance. The performance is increased in terms of speed of loading page and decreasing the server load. If you are looking for one of the best caching plugin, and for free, the W3TC should be your first choice. But you have to do an additional task if you are going to install this plugin and use it on your blog which is hosted in a server that is running Nginx. This additional task has to be performed in order to add certain configuration to the virtual hosts file of Nginx, so that the same function can be reflected as what in Apache rewrites rules (in .htaccess file). Apache .htaccess file is not the same as Nginx virtual hosts file.


You need a working server that should be running Nginx web server. I am using Ubuntu in this tutorial. And moreover you need basic knowledge on how to use WordPress, and how to use SSH on your server, and you should also have Nginx on your server.

Making Nginx configuration for W3 total cache plugin

Step 1- The first thing is that you have to login to your server through any SSH client that you like. I am a Windows user so I am using Putty in this tutorial. It will be better if you login with root privilege, once you have logged in. if you use Ubuntu, then you can input “su” and then press enter.


Step 2- Now for the domain name of your blog, you have to edit the virtual hosts file. You can do this by entering the following command:

nano /etc/nginx/sites-available/

In the above line, you have to replace or change the with your own domain name. I am using htdocs in this tutorial, but you can also replace it with public_html if you use it as your web root directory.


Step 3- But if you are using the default one, then all you have to do is to clear the whole thing out, and then you have to paste the one written below:

server { server_name; access_log /var/log/nginx/; error_log /var/log/nginx/ debug; root /var/www/; 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; } }

When you are done completing the whole thing, then you have to press Control+O, to save the changes, and then press Control+X, so that you can exit the editor. After that, you should be able to see the screen which will look something like this:


Step 4- Now the next thing is that you have to restart the Nginx service and also test the new configuration as well. You can perform this task by entering the following command:

nginx –t && service nginx restart


Step 5- The next step is that you have to login to your WordPress Admin Dashboard. After logging in, you have to go to General settings page of W3TC.


Step 6- Now the first thing is that you have to go to the section of Page Cache. First you have to enable that option, and then from the drop down menu, you have to choose the option which says Disk Enhanced.


Step 7- After doing the above step, you have to scroll down a little bit. There will be a section called Minify. You have to make sure that it is disabled. The reason is that, I think, all the other themes and plugins do not support this feature. But if you are completely sure that the theme that you are using, it can be minified, then you can enable it if you wish.


Step 8- Now you have to move on to the next section, which is called Database Cache. You have to enable the Database Cache option, and then you have to choose the option which says Opcode: Alternative PHP Cache (APC).


When you save it, then the option of APC storage will be offered by W3 Total cache, and that option will be for Minify cache and Page cache as well. BUT YOU DON’T USE IT. For Page Cache and Minify Cache, Disk Enhanced is better.

Step 9- Now you have to do the same things with the option of W3TC’s Object Cache.


Step 10- Now the Browser Cache section will come next. At nginx-level, the Nginx configuration takes care of browser cache very well, so you can disable this option. The cache duration of 25-years is already set up by it. Nothing is wrong with the Browser Cache option, which is provided by the W3TC plugin. But nginx can handle it itself and faster.


Step 11- After that, CDN (Content Delivery Network) is the next section. According to your desire, you can enable or disable it. But you have to make sure that you fully understand on how to use it. At Nginx level, it requires no configuration.

Step 12- I don’t use Varnish Cache in my server, so here I simply disa3ble the Reserve Proxy option.

Step 13- You can set all of the other options according to your choice. Once you have done everything, don’t forget to press the Save All Settings button.

So this is it. If you read this tutorial from start till the end then you know every step on how to setup Nginx Virtual Host for WordPress with W3 Total Cache Plugin.

I hope you got a lot of help from this tutorial.

Hope to see you soon. Thanks. !!

Article Tools

Did this article help you?


an Author!

Write an Article