Gzip compression reduces the size of text responses sent to a browser. This guide explains how an administrator can enable it in Apache or NGINX and check a response from the website.

Importance of enabling Gzip compression

HTML, CSS and JavaScript often compress well. Already compressed files, such as JPEG images and ZIP archives, usually gain little. Compression uses processing time, so test the effect on your application.

These examples are for independently managed Linux web servers. On a control-panel server, use its supported configuration process so generated settings do not overwrite your changes. Back up the configuration before editing it.

Enable Gzip compression in Apache

  1. Check that mod_deflate is loaded:
sudo apachectl -M
  1. If needed on Debian or Ubuntu, enable the module with sudo a2enmod deflate. Other distributions use their own package and module configuration.
  2. Add this to the intended site’s Apache configuration, or to its .htaccess file where the server permits these directives:
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/javascript application/json application/xml text/xml image/svg+xml
  1. Validate the configuration:
sudo apachectl configtest
  1. After a successful check, reload Apache using the service name for your distribution. Debian and Ubuntu normally use sudo systemctl reload apache2; Red Hat-based distributions normally use sudo systemctl reload httpd.

Enable Gzip compression in NGINX

Add or update these directives in the intended server block, retaining the site’s other settings:

gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_vary on;
gzip_types text/plain text/css text/javascript application/javascript application/json application/xml text/xml image/svg+xml;

NGINX includes text/html automatically. Avoid duplicate directives in the same block.

Test the configuration, then reload only if the test succeeds:

sudo nginx -t
sudo systemctl reload nginx

Check a response

Replace the URL with a text page served by your website:

curl -sS -D - -o /dev/null -H 'Accept-Encoding: gzip' https://example.com/

Look for Content-Encoding: gzip. A proxy or CDN may handle compression, and small responses or excluded content may not be compressed. Test the actual page with a GET request rather than relying on a HEAD response.

Where a response combines secrets with user-controlled content, review compression with the application developer because HTTPS compression can expose information through BREACH attacks.