0
0
Nginxdevops~5 mins

Gzip compression in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Websites often send large files that take time to load. Gzip compression makes these files smaller before sending, so pages load faster and use less internet data.
When your website has large HTML, CSS, or JavaScript files that slow down loading.
When you want to improve user experience by speeding up page load times.
When you want to reduce bandwidth costs by sending smaller files.
When you want to optimize your site for mobile users with slower connections.
When you want to improve your website's SEO by having faster loading pages.
Config File - nginx.conf
nginx.conf
http {
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_min_length 256;
    gzip_comp_level 5;
    gzip_vary on;
}

This configuration enables gzip compression in nginx inside the http block.

  • gzip on; turns gzip compression on.
  • gzip_types lists the file types to compress.
  • gzip_min_length sets the minimum file size to compress (256 bytes here).
  • gzip_comp_level sets compression level (1-9, 5 is a good balance).
  • gzip_vary on; adds a header so caches know the content is compressed.
Commands
Check the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new gzip compression settings without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a request to the server asking for gzip compressed content and check the response headers.
Terminal
curl -H "Accept-Encoding: gzip" -I http://localhost
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Content-Encoding: gzip Content-Type: text/html
Key Concept

If you remember nothing else from this pattern, remember: enabling gzip compression in nginx reduces file sizes sent to users, speeding up website loading.

Common Mistakes
Not enabling gzip with 'gzip on;' in the config.
Without this, gzip compression will not work even if other settings are present.
Always include 'gzip on;' inside the http block to activate compression.
Forgetting to reload nginx after changing the config.
Changes won't take effect until nginx reloads the configuration.
Run 'sudo systemctl reload nginx' after editing the config.
Not specifying the correct file types in 'gzip_types'.
Only files matching these types will be compressed; others will not.
Include all relevant MIME types like text/css, application/javascript, and application/json.
Summary
Edit nginx.conf to enable gzip compression and specify file types to compress.
Test the configuration syntax with 'nginx -t' before applying changes.
Reload nginx to apply the new settings without downtime.
Verify gzip is working by checking response headers with curl.