How to optimize your site using Gzip compression on the Nginx server
My test platform: CentOS Linux release 7.6.1810 (Core)
Requirements: user with root privileges or non-root user with sudo privileges.
Edit the configuration file /etc/nginx/nginx.conf and add as shown below:
server {
[...]
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]\.";
...
}
Explanation for the configuration:
- gzip on - enables gzip compression,
- gzip_vary on - tells proxies to cache both gzipped and regular versions of a resource,
- gzip_min_length 1024 - informs Nginx to not compress anything smaller than the defined size,
- gzip_proxied - compress data even for clients that are connecting via proxies,
- gzip_types - enables the types of files that can be compressed,
- gzip_disable “MSIE [1-6]."; - disable compression for Internet Explorer versions 1-6.
Save the file, and then restart the Nginx service for the changes to take effect:
$ sudo nginx -t
$ sudo systemctl restart nginx.service
$ systemctl status nginx.service
GZIP compression improves page loading speed and SEO test results, e.g. Google’s PageSpeed Insights and GTmetrix.
Reference: NGINX Documentation