How to enable gzip compression in nginx server on Linux or Unix system

I am a new Linux system admin and how can I enable gzip compression on nginx web server for JS/CSS/HTML files? How can I enable gzip/deflate in Nginx on Ubuntu Linux server? How do I configure gzip compression with NGINX web server on FreeBSD Unix server?

You need to use the ngx_http_gzip_module module. It compresses all valid HTTP responses (files) using the “gzip” method. This is useful to reduce data transfer size and speed up web pages for static assets such as JavaScript, CSS files and more. This page explains how to enable the gzip/deflate in nginx running on Linux or Unix-like systems.

Steps to enable gzip compression in nginx server

Edit your nginx.conf file or create a new config file called /etc/nginx/conf.d/static_gzip.conf:
$ sudo vi /etc/nginx/nginx.conf
Add the following in http context:

        ##
        # Gzip Settings
        ##
 
        gzip on;
        gzip_disable "msie6";
 
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        # Specify the minimum length of the response to compress (default 20)
        gzip_min_length 500;

Save and close the file. Verify that there are no errors in config file:
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the nginx server

Type the following command to restart or reload nginx server:
$ sudo service nginx reload
OR
$ sudo systemctl reload nginx
OR
$ sudo /etc/init.d/nginx reload

How do I verify gzip/deflate working or not?

Use the following syntax:
$ curl -I -H 'Accept-Encoding: gzip,deflate' https://your-domain-here/file.css
$ curl -I -H 'Accept-Encoding: gzip,deflate' https://s0.cyberciti.org/assets/auto/cms/wp-content/cache/autoptimize/css/autoptimize_4c2bea242e2386438912dd88773b352c.css
$ curl -I -H 'Accept-Encoding: gzip,deflate' https://www.cyberciti.biz/

Sample outputs:

HTTP/1.1 200 OK
Server: nginx
Date: Sun, 05 Mar 2017 18:45:31 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Whom: l1-com-cyber
Strict-Transport-Security: max-age=15768000; includeSubdomains
Link: ; rel="https://api.w.org/"
X-Varnish: 1812270 1794298
Age: 475
Via: 1.1 varnish-v4
Front-End-Https: on
Content-Encoding: gzip

If your origin Nginx server is behind a CDN/WAF, try:
$ curl -H 'Accept-Encoding: gzip,deflate' \
--resolve s0.cyberciti.org:YOUR-ORIGIN-Public-IPv4-HERE \
-H 'Host: s0.cyberciti.org' https://s0.cyberciti.org/

Understanding nginx ngx_http_gzip_module options

  1. gzip on : Enables or disables gzipping of responses under Nginx.
  2. gzip_disable "msie6" : Disable gzipping for Microsoft IE6 client.
  3. gzip_vary on : Enables or disables inserting the “Vary: Accept-Encoding” response header. This header inform the browsers if the client can handle the compressed version of the website or not especially when your Nginx server is behiend CDN or another reverse caching server.
  4. gzip_proxied any : Make sure we enables compression for all proxied requests.
  5. gzip_comp_level 6 : We can set a gzip compression level of a response. Acceptable values are in the range from 1 (min) to 9 (max).
  6. gzip_buffers 16 8k :
  7. gzip_http_version 1.1 : Sets the minimum HTTP version of a request required to compress a response.
  8. gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript : Enables gzipping of responses for the specified MIME types in addition to “text/html”. The special value “*” matches any MIME type since Nginx version 0.8.29. Responses with the “text/html” type are always compressed.

Nginx gzip vs plain text size

First, download a plain text file:
$ URL="https://www.cyberciti.biz/"
$ curl -s --output text $URL

Next, download a gzipped text file:
$ curl -s --output compressed -H 'Accept-Encoding: gzip,deflate' $URL
Compare file size using the ls command:
$ ls -l text compressed
$ ls -lh text compressed
$ file text compressed

Comparing file sizes plain/text with compressed file.

Conclusion

In this tutorial we explained how to enable and use gzip filter with Nginx web server that compresses responses. This often helps to reduce the size of transmitted data by half or even more. In other words, your site will load faster. For more information see ngx_http_gzip_module docs here. The next part of the tutorial will show you how to enable and use Brotli compression with Nginx.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • RajK Apr 13, 2017 @ 20:06

    Hey, thanks for this info. I just used it on my blog.

  • G Sep 1, 2020 @ 12:39

    Hi Vivek, in this instance you probably should not be recommending using compression with SSL due to this opening up the BREACH vulnerability.

    • T49675 Sep 2, 2020 @ 5:11

      @G,

      I think this is safe as config enabled gzip only for static assets:
      gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

      For what I read you need to avoid gzip for sensitive information such as login session.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.