How to Setup Nginx Virtual Host for WordPress with WP Super Cache
Edit Article

  • 1 Editor

If you want to setup Nginx virtual host for WordPress with WP super cache, then you have come to the right place. In this article, I will be telling you the whole procedure through which a virtual host can be setup with WP super cache. I will show you a working configuration for your Nginx virtual hosts so that your WordPress-based blog can use WP Super Cache plugin. It is a catching and a very fast engine for WordPress. And in order to make your server lighter and load your web site much faster, it produces static html files.


Note that whenever I say server, it will mean a dedicated server or a VPS.


So now, let us get started with the whole procedure.



Requirements

The first thing is that you need a working server which is running Nginx web server. I am using Ubuntu in this article. You should have basic knowledge about WordPress. You should also know how to perform SSH operations on your server. You should also have a server with Nginx on it. The WPSC plugin should also be installed in your blog.

Making Nginx configuration for WP super 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. It would be better if you login with root privilege, once you have logged in. If you are using Ubuntu, then you can simply type “su” command and then press enter.

1

Step 2- Now for the domain name of your blog, you have to edit the virtual hosts file. For this purpose, you can enter the following command:


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


Note: You have to replace or change “domain.com” by the domain name of your own web site. I am using “public_html”, but you can also use “htdocs” instead of public_html, if you use it as your own web root directory.

2

Step 3- So if you are still using the default one, then you can simply clear all of it, and then replace it with the 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;

  1. 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';


}



  1. 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';


}



  1. 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';


}



  1. Use cached or actual file if they exists, otherwise pass request to WordPress

location / {


try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;


}


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;


}



  1. 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 this task is done, now you have to press Control+O so that it can be saved, and then press Control+X so that you can exit the editor. Now you will see a screen like this:

3

This configuration for virtual hosts file of Nginx also supports WPSC option for Direct browser cache, Static Page Caching using Disk for static content like css, js, images, etc and moreover there is no Mobile user agent checks. So it means that now you are going to have to rely on theme with responsive design.


Step 4- now the next thing is that you have test the new configuration and then you have to restart Nginx service as well. It can be performed by using this command:


nginx –t && service nginx restart

4


Step 5- Now in this step, you have to login to your WordPress Admin Dashboard. Then you have to go to Settings and then WPSC Settings page, once you are logged in.

5

Step 6- When you reach in the WPSC settings page, now click on the “Advanced” tab, and then you have to enable or tick mark the option which says “Use mod_rewrite to serve cache files. (Recommended)”. Once the above mentioned things are done, then you have to press the button which says “Update Status”.

6

Step 8- You can also disable the option which says “Mobile device support”, because this option is not needed with this setting. You can do so by going back to the “Advanced” tab.

7

So this is it. If you have followed all these steps then you have successfully setup the Nginx Virtual Host for WordPress with WP super cache.

Article Tools

Did this article help you?

YesNo

Become
an Author!

Write an Article