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.
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”.
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..
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
Step 5 – Then login to your WordPress Admin Dashboard. Once you logged in, go to Settings then WPSC Settings page.
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.
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.
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.
That’s it. I hope it works well for your blog. Have a question or have something to say? Drop me a comment below..
mbak sawiyati, abis install wp supercahce blank screen knp kah? pake nginx udah sesuai confignya mbak. abis nambahin : define(‘WP_CACHE’, true); di wp-config trus blank. tolong solusinya mbak…
Hi,
I’m a little curious about these line of code:
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
}
How is that going to work for the home page? It seems to me that it will try to fetch /wp-content/cache/supercache/example.com//index.html OR worse yet: /wp-content/cache/supercache/example.com///index.html
and then go on to the $uri … so the home page will never be accessed from the cache. Is that right, or have I missed something?