Nginx precompress resources for saving cpu
Published on 08.06.2014
Nginx have one great module "HttpGzipStaticModule", according to documentation on nginx website his job is to
Before serving a file from disk to a gzip-enabled client, this module will look for a precompressed file in the same location that ends in ".gz". The purpose is to avoid compressing the same file each time it is requested.
So the basic idea is to avoid CPU overhead of deflating on each request. Alson this will give us ability to use maximum compression of resources.
For using this module we must add something like that
location ~* \.(js|css|json)$ { gzip_static on; }in our config file in section
server
.
In my opinion compressing with gzip image files like (jpg/png/gif) are stupid because they already compressed. Gziping them will slow down the browser. My advice is to use "convert" from imagemagick package to stripping them on deploying.
Here are some example for gzip css/js/json files in deployment process.
find -L "/projects/***********/root/" -regextype posix-extended \( -type f -or -type l \) -and -regex '.*\.(css|js|json)' -and -size +1k | xargs -n1 -I "{}" sh -c 'gzip -n -c -9 {} > {}.gz && touch {} {}.gz'