How to Setup Caching on IIS

A comprehensive guide to configuring caching and compression in IIS for optimal performance.

Prerequisites

Before configuring caching on IIS, ensure you have:

  • IIS installed and running
  • Access to IIS Manager
  • Basic understanding of HTTP caching concepts
  • Administrator access to the server

Basic Caching Configuration

Cache Configuration
<configuration>
    <system.webServer>
        <staticContent>
            <clientCache cacheControlMode="UseMaxAge" 
                         cacheControlMaxAge="365.00:00:00" />
        </staticContent>
    </system.webServer>
</configuration>

Key settings explained:

  • cacheControlMode: Defines how cache headers are set
  • cacheControlMaxAge: Sets the maximum age for cached content
  • staticContent: Configures caching for static files

Cache-Control Headers

Advanced Cache Control
<configuration>
    <location path="static">
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <add name="Cache-Control" 
                         value="public, max-age=31536000, immutable" />
                </customHeaders>
            </httpProtocol>
        </system.webServer>
    </location>

    <location path="api">
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <add name="Cache-Control" 
                         value="no-store, no-cache, must-revalidate" />
                    <add name="Pragma" value="no-cache" />
                </customHeaders>
            </httpProtocol>
        </system.webServer>
    </location>
</configuration>

Compression Settings

<configuration>
    <system.webServer>
        <urlCompression doStaticCompression="true" 
                        doDynamicCompression="true" />
        <httpCompression>
            <staticTypes>
                <add mimeType="text/*" enabled="true" />
                <add mimeType="application/javascript" enabled="true" />
                <add mimeType="application/json" enabled="true" />
                <add mimeType="application/xml" enabled="true" />
                <add mimeType="image/svg+xml" enabled="true" />
            </staticTypes>
        </httpCompression>
    </system.webServer>
</configuration>

Compression settings explained:

  • doStaticCompression: Enables compression for static files
  • doDynamicCompression: Enables compression for dynamic content
  • staticTypes: File types to compress

Configuration Generator

Generated Configuration
# IIS configuration will appear here

Best Practices

Recommended Practices
  • Static Assets:
    • Use long cache times (1 year) with versioned URLs
    • Add Cache-Control: public for CDN caching
    • Enable compression for text-based assets
  • Dynamic Content:
    • Use shorter cache times
    • Implement cache busting for updates
    • Consider using no-cache for user-specific content
  • API Responses:
    • Use no-store for sensitive data
    • Implement ETag headers for validation
    • Consider using Vary header for different responses