How to Add CDN(Content delivery network) on Windows IIS server With Outbound Rules.



Step 1 :
Create the folder location on your computer and Create New Side on IIS server with that folder location.now all you all content on that folder.

Step 2 :
Add web.config file on root of the project directory.

Outbound Rules are applied in the output stream. it is important to define what we want to rewrite. we basically need to choose all the .css(Style sheet), and .js(javascript file) Files

For convenience request are rewritten to different host headers like (subdomain, IISBinding), etc. but we also need to rewrite requests for images also

Outbound Rules
<rule name="sites-01-css" preCondition="CheckHTML" stopProcessing="true"> 
 <match filterByTags="Link" pattern="http(s)?://www.(surendra.net.np)/(.*\.css)" />  
 <action type="Rewrite" value="http{R:1}://staticcdn.{R:2}/{R:3}" />
 </rule>

<rule name="sites-01-js" preCondition="CheckHTML" stopProcessing="true">
  <match filterByTags="Script" pattern="http(s)?://www.(surendra.net.np)/(.*\.js)" />
  <action type="Rewrite" value="http{R:1}://staticcdn.{R:2}/{R:3}" />
</rule>


Step 3:

This is not enough we also need to define Preconditions rules

If the rule that modifies HTML content, only HTTP responses with a content-type header set to “text/HTML” should be evaluated against this rule. Outbound rules evaluation and content rewriting is a CPU intensive operation that may negatively affect the performance of a web application. Therefore, use preconditions to narrow down the cases when outbound rules are applied. (source: iis.net)
 <preCondition name="CheckHTML">
        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> 
     </preCondition> 
Step 4 :
 xml version="1.0" encoding="UTF-8"?>
<configuration>
   <system.webServer>
    <rewrite>
   <outboundRules rewriteBeforeCache="true">
     <rule name="sites-01-css" preCondition="CheckHTML" stopProcessing="true">
       <match filterByTags="Link" pattern="http(s)?://www.(surendra.net.np)/(.*\.css)" />
       <action type="Rewrite" value="http{R:1}://staticcdn.{R:2}/{R:3}" />
     </rule>   
     <rule name="sites-01-js" preCondition="CheckHTML" stopProcessing="true">
       <match filterByTags="Script" pattern="http(s)?://www.(surendra.net.np)/(.*\.js)" />
       <action type="Rewrite" value="http{R:1}://staticcdn.{R:2}/{R:3}" />
     </rule>
    <preConditions>
       <preCondition name="CheckHTML">
         <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
       </preCondition>
     </preConditions>
   </outboundRules>
</rewrite>
</system.webServer>
   <system.net>
     <mailSettings>
       <smtp from="support@wallbee.dk"/>
     </mailSettings>
   </system.net>
</configuration>

The rewriteBeforeCache = "true" is important in situations where IIS user mode caching is used

Step 5: This is not Enough we also need to do other steps for font files.

Enabling CORS on IIS for only Font files
<rule name="Enable CORS for Fonts">
         <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
         <conditions>
           <add input="{REQUEST_URI}" pattern="^[^\?]+\.(ttf|otf|eot|woff|woff2|svg)(\?.*)?$" />
         </conditions>
         <action type="Rewrite" value="*" />
     </rule>

Step 6 :

Now your CDN host is ready to use from any of the origin site.

Post a Comment

0 Comments