Setup Nginx Virtual Host For WordPress With WP Super Cache

Saturday, July 23rd 2016. | how to

#howto ,#tutorial #VPS – Setup Nginx Virtual Host For WordPress With WP Super Cache – This tutorial explains step by step guide (with pics) about Setup Nginx Virtual Host For WordPress With WP Super Cache

Looking for Nginx Configuration For WP Super Cache setting? In this page you’ll see a working config for your Nginx virtual hosts to make your WordPress-based blog can use WP Super Cache plugin, a very fast caching engine for WordPress that produces static html files to make your site loads faster and make your server lighter. Previously, I also posted how to use W3 Total Cache plugin with WordPress on Nginx server environment. Today, you’ll see similar guide but this one is for its best competitor, the WP Super Cache which we will then simply call it as WPSC (just to make it simple).

p.s: Every time I say “server” it can refer to either VPS or Dedicated server. So don’t be confused.

I will not again explain what is WPSC and is it better than W3TC, Hyper Cache, or any other similar plugins available out there. You decide it by yourself. Also, in this post I will never compare which one is better. I believe everything has its own plus and minus.

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. Also make sure you’ve installed WPSC plugin in your blog.

How to configure Nginx Configuration For WP Super 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/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;
	}

	# 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..

wordpres wp super cahce config for nginx

About the configuration:

That config for Nginx virtual hosts file supports WPSC option for Static Page Caching using Disk, Direct browser cache for static content like images, js, css, etc and there is no Mobile user agent checks. It means, you have to rely on theme with responsive design, the most common web design in nowadays iPhone and Android era. Yup, you better ditch using dual themes on your blog as what Google recommends.

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 Settings then WPSC Settings page.

click wp super cache

Step 6 – Once you are in WPSC settings page, click the “Advanced” tab then tick mark / enable the “Use mod_rewrite to serve cache files. (Recommended)” option. Once done hit the “Update Status” button.

wpsc setting page

Step 7 – Once done, you can now Enable the caching function (if you haven’t done it yet) by going to the “Easy” tab, tick mark / enable the “Caching On (Recommended)” option then hit the “Update Status” button.

enable cache

Step 8 – You may also go back to “Advacned” tab and disable the “Mobile device support.” option as that’s not needed with this setting.

disable mobile agent check

That’s it. I hope it works well for your blog. Have a question or have something to say? Drop me a comment below..

This post Setup Nginx Virtual Host For WordPress With WP Super Cache is part of ServerMom.

Source link