views:

4

answers:

0

I make use of version numbers to cache static files, with URLs like this:

http://example.com/css/example.v123.css

Combined with a nginx config snippet like this:

# Serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
    root /var/www/example.com/static/;
    access_log off;
    if ($request_filename ~* ^(.*\.)v[0-9.]+(css|js|gif|png|jpg|jpeg)$) {
        rewrite ^(.*\.)v[0-9.]+(css|js|gif|png|jpg)$ /$1$2 last;
        expires max;
    }
}

This works nicely, but my question is whether this is optimal. In the nginx snippet, I am testing for the pattern, then rewriting the pattern in the next line. Is there a way to do this in one operation, so I don't have to do 2 regexes? In truth, it probably won't matter in real terms as loads won't be that high, but I'm curious as to whether there is a terser solution.

In Apache, I previously used:

RewriteRule ^/(.*\.)v[0-9.]+\.(css|js|gif|png|jpg)$ /$1$2 [L,E=VERSIONED_FILE:1]

Header add "Expires" "Mon, 28 Jul 2014 23:30:00 GMT" env=VERSIONED_FILE
Header add "Cache-Control" "max-age=315360000" env=VERSIONED_FILE

Which rewrote and tagged it as versioned.