Hi there,
I'm trying to get nginx to set expires headers only for assets that have
timestamps at the end of them but am having trouble figuring out exactly
what to put in the conf file. Would anybody be able to help me out?
Here's what I've tried:
# Trying to set expires headers for /test.png?1265332681 but not for
/test.png
# Works for /test.png?1265332681 but also matches /test.png
location ~* \.(htc|ico|css|js|gif|jp?g|png)(\?[0-9]+)?$ {
expires max;
break;
}
# No expires headers set
location ~* \.(ico|css|js|gif|jp?g|png)(\?[0-9]+)$ {
expires max;
break;
}
Thanks,
Tim
on 2010-02-07 02:52
on 2010-02-07 03:44
Hello! On Sat, Feb 06, 2010 at 05:51:12PM -0800, Tim Wong wrote: > # Works for /test.png?1265332681 but also matches /test.png > location ~* \.(htc|ico|css|js|gif|jp?g|png)(\?[0-9]+)?$ { Location match on path only, without query string, so this won't work. Just a side note: probably you mean "...|jpe?g|..."? > expires max; > break; Another side note: using "break" is pointless here, it just wastes CPU cycles. > } > > # No expires headers set > location ~* \.(ico|css|js|gif|jp?g|png)(\?[0-9]+)$ { > expires max; > break; > } Try this instead: location ~* \.(ico|css|js|gif|jpe?g|png)$ { if ($args) { expires max; } } Note well: this will work, but you may find it doing strange things once you add more directives into the above location, see http://wiki.nginx.org/IfIsEvil for details. Most generic solution is to use something like this: location ~* \.(ico|css|js|gif|jpe?g|png)$ { error_page 405 = @expiresmax; recursive_error_pages on; if ($args) { return 405; } ... } location @expiresmax { expires max; } Maxim Dounin
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.