Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get NGINX to serve .gz compressed asset files

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?

like image 675
matsko Avatar asked Aug 05 '11 07:08

matsko


People also ask

Does nginx support gzip?

You can configure Nginx to use gzip to compress the files it serves on the fly. Those files are then decompressed by the browsers that support it upon retrieval with no loss whatsoever, but with the benefit of a smaller amount of data to transfer between the web server and browser.

How do I know if gzip is enabled nginx?

You can tell using Developer Tools (F12). Go to the Network tab, select the file you want to examine and then look at the Headers tab on the right. If you are gzipped, then you will see that in the Content-Encoding.

How do you implement gzip compression?

Gzip on Windows Servers (IIS Manager)Open up IIS Manager. Click on the site you want to enable compression for. Click on Compression (under IIS) Now Enable static compression and you are done!


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.

like image 168
Anatoly Avatar answered Oct 06 '22 17:10

Anatoly