Strong caching with NGINX

2nd February 2014 | by Adam Beres-Deak | nginx, caching

Strong caching is very important nowadays since it can reduce page load times for the users (and eventually it can also reduce network transfer costs for the publishers). Here we see a simple example how to do it with NGINX for static files like css, JavaScript and images.

server {
    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        root /var/www/mysite;
        expires max;
        add_header Cache-Control "public";
        access_log off;

Of course after modifying the config file we have to reload the configs (e. g. on Ubuntu we have to type sudo /etc/init.d/nginx reload)

There are a few things to note here:

Have you already enabled strong caching for yout asset files?

Update: Maybe you noticed the lookahead in the regex. It has a reason to be there: this way the regex engine doesn't create a capture group (no $1) so it has better performance.

by Adam Beres-Deak

| Share | Tweet | Share | Share

Latest blog posts

Displaying icons with custom elements 14th October 2015

Plain JavaScript event delegation 26th January 2015

After the first year of blogging - what happened on my blog in 2014? 1st January 2015

Better webfont loading with using localStorage and providing WOFF2 support 18th December 2014

Worth watching: Douglas Crockford speaking about the new good parts of JavaScript in 2014 20th October 2014