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
- gzip on : Enables or disables gzipping of responses under Nginx.
- gzip_disable "msie6" : Disable gzipping for Microsoft IE6 client.
- 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.
- gzip_proxied any : Make sure we enables compression for all proxied requests.
- 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).
- gzip_buffers 16 8k :
- gzip_http_version 1.1 : Sets the minimum HTTP version of a request required to compress a response.
- 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.
🐧 3 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Hey, thanks for this info. I just used it on my blog.
Hi Vivek, in this instance you probably should not be recommending using compression with SSL due to this opening up the BREACH vulnerability.
@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.