In this tutorial we offer configuration examples that will help you set a custom public cache time on your origin. This will allow you to control how long files are cached by browsers and intermediate caches like CDNs.

First, a Quick Review of Cache Control

When a user accesses your website, files start loading in their browser. Each of these files have some unique properties, one being Cache-Control.

You can look at the cached file as a way of storing a copy of that file in a web server or browser. So when a new request comes in for the same file, the web server or browser can serve that file straight from it’s local cache. This is much more efficient than reaching out to the origin server to fetch it again.

There are a couple important directives related to the Cache-Control header. These directives determine who should store that file, under what circumstances it should be stored, and for what time period it should be stored.

The public value of Cache-Control allows any browser or web server to store that file in its cache:

Cache-Control: public

The private value, on the other hand, allows the browser to cache the file. But the file CAN NOT be cached by any intermediate cache such as a CDN.

Cache-Control: private

The no-cache value says that neither a browser nor any intermediate cache can cache that file. Basically, it CAN NOT be cached by any mechanism.

Cache-Control: no-cache

Custom Cache Configuration Examples

Below we provide configuration examples for Apache, Nginx, Lighttpd, and IIS 7. The goal in each of these examples is to get the Cache-Control header to read:

Cache-Control: public, max-age=12345

Apache

  1. Make sure that the mod_headers module is enabled:

    a2enmod headers

  2. Open your virtual server conf file:

    nano /etc/apache2/sites-available/mysite

  3. Add the custom caching headers for the file types you wish:
    <FilesMatch "\.(css|exe)$">
    Header set Cache-Control max-age=12345
    </FilesMatch>
  4. In order for changes to take effect, you need to restart the server:

    sudo service apache2 restart

Nginx

  1. Navigate to your /sites-available/mysite and add the code below based on the file types you wish:
    location ~* \.(css|exe)$ {
      add_header Cache-Control "public, max-age=12345";
    }
  2. In order for changes to take effect, you need to reload the server:

    nginx -s reload

Lighttpd

  1. Open up your lighttpd.conf file.
  2. Enable mod_setenv by adding it to the beginning of server.modules:
    server.modules = (
        "mod_setenv"
        "mod_expire",
        "mod_rewrite",
        "mod_alias",
        "mod_access",
        "mod_fastcgi",
        "mod_accesslog"
    )
  3. Add the following block in your conf file based on the file types you wish:
    $HTTP["url"] =~ "\.(css|exe)$" {
        setenv.add-response-header = ( "Cache-Control" => "max-age=12345, public" )
    }
  4. In order for changes to take effect, you need to reload the server:

    service lighttpd reload

IIS 7

  1. Select the folder that you want to enable caching for and double click HTTP header:

    IIS 7 response headers

  2. After that, proceed with enabling the caching for that particular folder:

    IIS 7 response headers

If you have any questions about setting the cache time from your origin, please leave a comment below.