Adding gzip file compression can speed up the response time for your website. As with many optimisations, it's not a silver bullet, as compressing files on the server consumes CPU effort. However, the download size of transferred files is reduced for your end users.
This guide will show you how to setup gzip compression for the nginx server.
Open up the main nginx config file, probably located at
http section within this file, and add in the following gzip
directives & values:
gzip onenables compression. It's actually off by default.
gzip_comp_levelsets the level of compression. 1-9. 1 being low, 9 high. Inevitably, the higher compression level, the more CPU cost.
gzip_typeslists the file/mime types to apply compression for.
text/htmlis already enabled by default, so there's no need to explicitly add it to the list here. In fact, nginx will warn you if you do this.
There's a useful mime type reference within the nginx installation, located at
Now just restart or reload nginx for the changes to take effect.
On future HTTP requests to the nginx server, seek out the response header named
Hopefully you'll observe something like
In Chrome dev tools, it might look something like this:
The nginx ngx_http_gzip_module module is responsible for the gzip compression support. To find out what other options are available, check out the official documentation.
Enjoy your gzipped compression. Your users certainly will.