Using W3 Total Cache With Wordpress on Nginx

December 12, 2013    wordpress nginx cache performance web

This post assumes you have already installed the following applications. If not, you can check out the articles on how to set them up -

  1. How To Install Linux, nginx, MySQL, PHP (LEMP) stack on Ubuntu 12.04
  2. How To Install Wordpress on Ubuntu 12.04

About W3 Total Cache

W3 Total Cache improves the user experience of your site by increasing server performance, reducing the download times and providing transparent content delivery network (CDN) integration.

Setup

The steps provided in this tutorial require that you have root priveleges on your server.

Step One - Editing the server configuration file

We will need to edit the server configuration file (or host file) for your server/domain.

To open the file, open terminal and type in these commands:
sudo nano /etc/nginx/sites-available/default

Step Two - Adding the required code in the host file

In your host file, you will find lines that start with “server{…}”. This is how a server/domain is defined on NginX. We will be adding our configuration codes into this block.

Add this block to your host file:

    server {  
    server_name yoursite.com www.yoursite.com;  
    
    access_log /var/log/nginx/yoursite.access.log;  
    error_log /var/log/nginx/yoursite.error.log debug;  
    
    root /var/www/yoursite.com/public_html;  
    index index.php index.html index.htm;
    
    set $cache_uri $request_uri;
    
    # PHP should handle all POST requests and URLs with a query string  
    if ($request_method = POST) {  
       set $cache_uri 'null cache';  
    }  
    if ($query_string != "") {  
       set $cache_uri 'null cache';  
    }  
    
    # Don't cache certain WP files  
    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 cache for logged in users and 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 it exists, else pass it on to WP  
    location / {  
        try_files /wp-content/cache/page_enhanced/${host}${cache_uri}_index.html $uri $uri/ /index.php?$args;  
    }  
    
    location ~ ^/wp-content/cache/minify/[^/]+/(.*)$ {  
        try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1;  
    }  
    
    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;  
    }  
    
    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;
        access_log off;  
        error_log off;  
    }  

Step Three - Save the file and reload NginX

Now that we have defined the necessary details in the host file, press Ctrl + X followed by Y to save the host file.

To finish, reload NginX to make the changes take effecte. Type in the following command in the terminal.
sudo service nginx reload



comments powered by Disqus