Rails 3.1 has a convenient system which can compress files into .gz files. However, instead what I've done is I've moved all the asset files that are created with assets:precompile to a static webserver. This all works, but how can I get nginx to serve the .gz files normally?
-
Since Rails 4.2 `rake assets:precompile` no longer creates .gz files . Some fixes https://multiplethreads.wordpress.com/2015/08/08/generate-gzip-assets-with-rails-sprockets-3/ – Nishant Sep 18 '15 at 20:51
1 Answers
1) ensure you have Nginx > 1.2.x (to proper headers modifications) and compile with --with-http_gzip_static_module option
2) Enable this option gzip on (to serve back-end response with gzip header)
3) Setup assets location with gzip_static on (to serve all.css.gz, all.js.gz files directly)
4) Prevent of etag generation and last-modify calculation for assets
5) Turn on the right Cache-control to cache SSL served static assets, unless they will be expired once browser is closed
location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
}
if you would like to get full Nginx configuration, you can see this gist on Github.
open_file_cache helps you to cache: open file descriptors, their sizes, modification times and directory lookups, which is helpful for high load on the file system.
UPDATE: If you are living on the edge, turn on the SPDY to boost the SSL connection.

- 15,298
- 5
- 53
- 77
-
1don't forget to turn off any logs for assets: access_log /dev/null; error_log /dev/null; – Anatoly Sep 09 '11 at 16:09
-
1
-
2You only need to use /dev/null if you want to turn off the error log (which you really shouldn't anyway). access_log accepts an 'off' argument which actually turns off logging, so the lines aren't even generated in the first place. – kolbyjack Dec 22 '11 at 12:07
-
2To minimize the load on file system, don't forget to mount a device with **noatime** option. It will prevent an update access time on each assets while access every time. – Anatoly Sep 03 '12 at 03:40
-
2Depending on your situation, you might want to remove images from your gzip list. It doesn't help much and eats CPU. Better compress them before uploading to the server. – pors Sep 10 '12 at 11:26
-
@Anatoly It's been a long time since you wrote this. Do we today need to turn off the headers `Last-Modified` and `ETag`? I am trying to figure out why that's necessary for serving pre-compressed files with `gzip_static on`. – Ethan Sep 05 '18 at 14:38
-
Not really required anymore, such micro optimisations become less and less noticeable – Anatoly Sep 05 '18 at 14:39
-
@Anatoly Yes, gzip_static is indeed today a micro optimization. But, if someone needed `gzip_static on`, was removal of `Last-Modified` and `Etag` required to support it? (or, you had mentioned that for some other purpose?). – Ethan Sep 05 '18 at 14:51