Speed Up WordPress on IIS Windows WP Super Cache


Speed up Wordpress on Windows IIS by using URL rewrite rules. Reverse proxy behavior like Squid and Varnish. Fetch WP Super Cache files directly.


It has been some time that I was struggling to speed up WordPress installed on Amazon EC2’s windows server. I did try the W3 Total Cache too before finally settling on WP Super Cache.

If you are running your blog on non-windows i.e. Linux machine, you probably would have already used the in-built features of these cache plug-ins to auto optimize. The problem comes into picture only on windows IIS.

Why WP Cache need special settings on Windows

The simple reason of slow speed is the editing of web.config vs Apache’s htaccess file. By default, the WP Super and W3 Total Cache are designed to automatically edit htaccess file, not the web.config.

I am sure you would have already implemented either of these cache and configured their best settings.
Hence, I am just going to focus primarily on the critical point of jam.

Before we jump to the solution, a little background on how these caches work would help you get a grasp of things.

How do WP Super Cache and W3 Total Cache work?

The way WP Super cache of W3 Total Cache work is that they both create the HTML versions of the posts and the pages.
WP Super cache stores these pages on the disk whereas W3 Total Cache keeps them in the memory.

They then, serve the user requests in the following order:

  1. User requests page www.am22tech.com from the internet browser.
  2. Request reaches your web server and hits IIS. IIS then routes the request to Worpdress PHP engine running in memory. This is a critical junction as it may consume about 1-2 seconds on shared or low memory (RAM) servers.
  3. This time can be saved if the web server can by-pass the wordpress PHP engine completely for the cached pages. By default, both cache plug-ins create the htcaccess file and embed the REWRITE rules to make this happen automatically for you.

    Since, we are on windows server with IIS, the plug-ins are not able to do this automatically for us.
    Hence, whenever we hit the IIS, it has to forward the call to WordPress PHP engine and then the cache plug-ins serve the page either from the disk or memory.

  4. So, the idea is to write these REWRITE rules ourselves in ISS web.config ourselves to completely skip the PHP processing.

Understood the concept? If yes, then here is what you need to do:

  1. Pre-requisite: You would need the URL Rewrite 2.0 extension for IIS. Most probably, you would already have it installed. If not, then please do it now.
  2. Edit the web.config file in your wordpress install. You would find this file in root directory.
  3. Add a new web.config file in the WP Super Cache folder add the Cache-control headers. This is optional but recommended.

IIS Windows WP Super Cache Configuration

  1. I am assuming that you already have WordPress and WP Super Cache plugin installed. If not, then please install it now.
  2. URL rewrite rules required for making WP Super cache work as reverse proxy
    Go to your wordpress root folder and add these in the web.config file. If you do not have already have this file, add a new one.

    <system.webServer>
       <rewrite>
    	<rules>
            	<rule name="WP super cache Windows WordPress" stopProcessing="true">  
    			<match url="(.*)" />  
    			<conditions logicalGrouping="MatchAll">  
    				<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />  
    				<add input="{QUERY_STRING}" pattern=".*=.*" negate="true" />  
    				<add input="{QUERY_STRING}" pattern=".*attachment_id=.*" negate="true" />  
    				<add input="{HTTP_COOKIE}" pattern="^.*(comment_author_|wordpress|wp-postpass_).*$" negate="true" />  
    				<add input="{DOCUMENT_ROOT}/wp-content/cache/supercache/{HTTP_HOST}/{R:1}index.html" matchType="IsFile" />  
    			</conditions>  
    				<action type="Rewrite" url="wp-content/cache/supercache/{HTTP_HOST}/{R:1}index.html" />  
    		</rule>
    	</rules>
    </rewrite>
    </system.webServer>
    
  3. (Optional) Create a new web.config file in wp-content\cache folder and add the following rules there. If you do not already have this file there, a add new one.
    <?xml version="1.0" encoding="UTF-8"?>  
    <configuration>  
    	<system.webServer> 
    		<httpProtocol>  
    			<customHeaders>  
    				<add name="Cache-Control" value="max-age=3000, must-revalidate" />  
    			</customHeaders>  
    		</httpProtocol>  
    	</system.webServer>  
    </configuration>  
    
  4. Save the file and you are all set.

Frequently Asked Questions

Don’t the WP super cache and W3 Total cache have this URL rewrite functions out of the box?
Yes, both of them have it. But, it is only available for Apache web server and not IIS.
They both can create these URL rewrite rules in htcaccess file automatically.

Since you are on windows and most probably using IIS web server, you would need web.config files for the URL rewrite to work.

How to test the speed gains?

You can immediately test the speed gains by using Google Webmasters ‘Fetch like Google’ feature.

Just add any of your post or page link and click fetch. You would see the total time it took Google to fetch that page. It should certainly be much lesser than what it was earlier.
This is simply because IIS is now diverting and serving all incoming calls to the WP Super Cached pages rather than invoking WordPress Engine.
This bypasses WordPress’s PHP engine out of picture completely.

Over a period of 2-3 days to a week, you will start noticing the substantial downfall in the page download time.

Speed gains?

If you believe, the difference was huge. I was able to reduce the page fetch time from 3 seconds to 0.03 seconds. That’s a world of a difference!

Google crawl stats WP super cache reverse proxy windows worpress

Google crawl stats shows the reduction in page load time with WP super cache on windows and worpress

All the posts are now served from the cache directly. WP Super cache works in the background and is never brought into front line processing. PHP engine is never directly touched if you already have a cached file.

You can share your suggestions and ask questions, if any in the comments section.


 Updated

 Tagged In


© Copyright   AM22 Tech
Follow Us
     


This story was originally published at AM22 Tech. This printer-friendly version is made available for your personal and non-commercial use only.
  
© Copyright 2017   AM22 Tech