[Nginx Best Practices] Gzip & Brotli Compression for Nginx
Configuring Gzip & Brotli Compression for Nginx
Configuring Gzip & Brotli compression on an Nginx web server running in Linux. Gzip & Brotli are the most popular compression algorithms supported by major web browsers.
- Gzip compression Algorithm
Gzip provides a lossless compression, this means the original data can be recovered when decompressing it. It is based on the DEFLATE algorithm, which is a combination of LZ77 and Huffman coding.
- Brotli compression Algorithm
Just like gzip, brotli is a lossless compression algorithm widely supported across many browsers. It is developed by Google and is best suited for compression of text-based static resources, like json, js,css, and html. We will use NGINX module for Brotli compression in this setup.
Gzip
Example
1 | # gzip |
gzip
1 | Syntax: gzip on | off; |
Enables or disables gzipping of responses.
gzip_buffers
1 | Syntax: gzip_buffers number size; |
Sets the number and size of buffers used to compress a response. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform.
gzip_comp_level
1 | Syntax: gzip_comp_level level; |
Sets a gzip compression level of a response. Acceptable values are in the range from 1 to 9.
gzip_min_length
1 | Syntax: gzip_min_length length; |
Sets the minimum length of a response that will be gzipped. The length is determined only from the “Content-Length” response header field.
1 | Syntax: gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...; |
Enables or disables gzipping of responses for proxied requests depending on the request and response. The fact that the request is proxied is determined by the presence of the “Via” request header field. The directive accepts multiple parameters:
-
off
disables compression for all proxied requests, ignoring other parameters;
-
expired
enables compression if a response header includes the “Expires” field with a value that disables caching;
-
no-cache
enables compression if a response header includes the “Cache-Control” field with the “no-cache” parameter;
-
no-store
enables compression if a response header includes the “Cache-Control” field with the “no-store” parameter;
-
private
enables compression if a response header includes the “Cache-Control” field with the “private” parameter;
-
no_last_modified
enables compression if a response header does not include the “Last-Modified” field;
-
no_etag
enables compression if a response header does not include the “ETag” field;
-
auth
enables compression if a request header includes the “Authorization” field;
-
any
enables compression for all proxied requests.
gzip_types
1 | Syntax: gzip_types mime-type ...; |
Enables gzipping of responses for the specified MIME types in addition to “text/html”. The special value “*” matches any MIME type (0.8.29). Responses with the “text/html” type are always compressed.
gzip_vary
1 | Syntax: gzip_vary on | off; |
Enables or disables inserting the “Vary: Accept-Encoding” response header field if the directives gzip, gzip_static, or gunzip are active.
Module ngx_http_gzip_module - http://nginx.org/en/docs/http/ngx_http_gzip_module.html
gzip_static Sending Compressed Files
To send a compressed version of a file to the client instead of the regular one, set the gzip_static directive to on within the appropriate context.
1 | location / { |
In this case, to service a request for /path/to/file, NGINX tries to find and send the file /path/to/file.gz. If the file doesn’t exist, or the client does not support gzip, NGINX sends the uncompressed version of the file.
Note that the gzip_static directive does not enable on-the-fly compression. It merely uses a file compressed beforehand by any compression tool. To compress content (and not only static content) at runtime, use the gzip directive.
1 | Syntax: gzip_static on | off | always; |
Enables (“on”) or disables (“off”) checking the existence of precompressed files. The following directives are also taken into account: gzip_http_version
, gzip_proxied
, gzip_disable
, and gzip_vary
.
Module ngx_http_gzip_static_module - http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html
$gzip_ratio
achieved compression ratio, computed as the ratio between the original and compressed response sizes.
1 | log_format compression '$remote_addr - $remote_user [$time_local] ' |
Remember to Reload NGINX to enable:
1 | nginx -t && nginx -s reload |
Brotli
Install Brotli
Install the Brotli module.
For CentOS 8.x, Oracle Linux 8.x, and RHEL 8.x:
1 | sudo yum install nginx-plus-module-brotli |
For CentOS 6.x and 7.x:
1 | sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm |
For Debian 10 and Ubuntu::
1 | sudo apt-get install nginx-plus-module-brotli |
For SLES 15:
1 | sudo zypper install nginx-plus-module-brotli |
Put the load_module
directives in the top‑level (“main”) context of NGINX Plus configuration file, nginx.conf:
1 | # /etc/nginx/nginx.conf |
Remember to Reload NGINX to enable the module:
1 | nginx -t && nginx -s reload |
Example
1 | # brotli |
Suggested MIME type
A more complete MIME type list has been suggested here:
1 | application/atom+xml |
References
[1] Module ngx_http_gzip_module - http://nginx.org/en/docs/http/ngx_http_gzip_module.html
[2] The gzip home page - https://www.gzip.org/
[3] Module ngx_http_gzip_static_module - http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html
[4] GitHub - google/brotli: Brotli compression format - https://github.com/google/brotli
[7] NGINX Docs | Brotli - https://docs.nginx.com/nginx/admin-guide/dynamic-modules/brotli/