Skip to content

Enable GZIP Compression on nginx Servers

Last updated on June 4, 2022

Enabling GZIP compression on Nginx servers is easy and it improves application performance as well as saves bandwidth transfer.

By following below steps you can enable GZIP on your virtual private server with simple configuration changes.

First step is edit nginx.conf file, that could be located /etc/nginx/nginx.conf or /usr/local/nginx/conf/nginx.conf, in most distributions.

Open nginx.conf file with your favorite editor. You can see that there’s already a block of settings regarding Gzip; you could always just modify those and un-comment out the lines as shown below –

# enable gzip compression

# Turns on/off the gzip compression.
gzip on;

# Compression level (1-9).
# 5 is a perfect compromise between size and cpu usage, offering about
# 75% reduction for most ascii files (almost identical to level 9).
gzip_comp_level    5;

# The minimum size file to compress the files.
gzip_min_length  1100;

# Set the buffer size of gzip, 4 32k is good enough for almost everybody.
gzip_buffers  4 32k;

# Compress data even for clients that are connecting to us via proxies,
# identified by the "Via" header (required for CloudFront).
gzip_proxied       any;

# This directive let you specify which file types should be compressed, in this case plain text, js files, xml and #css.
gzip_types    text/plain application/x-javascript text/xml text/css;

# Enables response header of “Vary: Accept-Encoding
gzip_vary on;

# end gzip configuration

Once your done with above config changes, restart or reload your server and you will now be serving site assets with gzip compression.

/etc/init.d/nginx reload
or
sudo service nginx restart

Apart from above benefits, Google takes site speed into account when ranking and placing your sites in their search engine.

To test if gzip is enabled, run:

curl -H "Accept-Encoding: gzip" -I https://arjunphp.com/

You should see content-encoding: gzip

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments