Skip to content

Instantly share code, notes, and snippets.

@zaius
Created April 10, 2012 03:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaius/2348131 to your computer and use it in GitHub Desktop.
Save zaius/2348131 to your computer and use it in GitHub Desktop.
Serve rails assets with etags from nginx
location / {
if ($uri ~* "/assets/.*-(.*)\..*") {
expires max;
add_header Cache-Control public
add_header Etag $1;
}
}
@malomalo
Copy link

You'll probably also want to add the following to serve the already compressed assets

gzip_static on;

@zaius
Copy link
Author

zaius commented Apr 14, 2012

Oh definitely. You can't put that in the if statement though, so you should define it globally at the top of nginx.conf.

@malomalo
Copy link

Ah I don't user an if statement. I have the following:

location ~ ^/assets/.*-(.*)\..* {
    gzip_static on; # to serve pre-gzipped version
    expires max;
    add_header ETag $1;
    add_header Cache-Control public;
    break;
}

@malomalo
Copy link

but looking at that doesn't make since. I don't know that that just applies to just that location

@zaius
Copy link
Author

zaius commented Apr 15, 2012

Pretty sure the gzip stuff can be applied per location, but I just have it defined globally. Doesn't seem to cause any problems to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment