Enable GZIP Compression in Nginx

393 words; 2 minute(s)

Table of Contents

Text Compression

Text compression allows a web server to serve text-based resources faster than uncompressed data. This can speed up things like First Contentful Paint, Tie to Interactive, and Speed Index.

Enable Nginx Compression with gzip

In order to enable text compression on Nginx, we need to enable it within the configuration file:

nano /etc/nginx/nginx.conf

Within the http block, find the section that shows something like the block below. This is the default gzip configuration I found in my nginx.conf file on Alpine Linux 3.17. Yours may look slightly different, just make sure that you're not creating any duplicate gzip options.

# Enable gzipping of responses.
#gzip on;

# Set the Vary HTTP header as defined in the RFC 2616. Default is 'off'.
gzip_vary on;

Remove the default gzip lines and replace them with the following:

# Enable gzipping of responses.
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]";

Explanations of ngx~httpgzipmodule~ Options

Each of the lines above enables a different aspect of the gzip response for Nginx. Here are the full explanations:

More information on these directives and their options can be found on the Module ngx~httpgzipmodule~ page in Nginx's documentation.