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.
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 its 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
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
- Make sure that the mod_headers module is enabled:
a2enmod headers
-
Open your virtual server conf file:
nano /etc/apache2/sites-available/mysite
-
Add the custom caching headers for the file types you wish:
<FilesMatch "\.(css|exe)$"> Header set Cache-Control max-age=12345 </FilesMatch>
- In order for changes to take effect, restart the server:
sudo service apache2 restart
Nginx
- Navigate to /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"; }
- In order for changes to take effect, reload the server:
nginx -s reload
Lighttpd
- Open your lighttpd.conf file.
-
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" )
-
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" ) }
- In order for changes to take effect, you need to reload the server:
service lighttpd reload
IIS 7
-
Select the folder that you want to enable caching for and double click HTTP header:
- After that, proceed with enabling the caching for that particular folder:
If you have any questions about setting the cache time from your origin, please feel free to reach out to support. Live chat and ticket support are available 24/7.