Operating System Linux

Enable Gzip Compression in Apache and Nginx on Ubuntu 24.04 and Debian 12

Turn on gzip compression for text responses in Apache and Nginx on current Ubuntu, Debian and RHEL 9 family servers, check that it works, and add Brotli.

Compressing text responses before they leave the server cuts the size of HTML, CSS, JavaScript and JSON considerably, which saves bandwidth and makes pages load faster. This guide shows how to enable and tune gzip compression in Apache 2.4 and in Nginx on Ubuntu 24.04 and 22.04, Debian 12 and 13, and the RHEL 9 family (AlmaLinux, Rocky Linux, CentOS Stream), how to confirm that it works, and how to add Brotli next to it.

Requirements

  • A server with Apache or Nginx already installed and serving a site.
  • A user that can run sudo.
  • curl on any machine, for testing.

What to compress and what to leave alone

Compress text formats: HTML, CSS, JavaScript, JSON, XML and SVG. Do not compress JPEG, PNG, WebP, video, ZIP archives or WOFF2 fonts. Those formats are already compressed, so the server would spend CPU time for no gain.

Enable gzip in Apache

Apache compresses responses with mod_deflate. Despite the name, it produces gzip-encoded responses.

Ubuntu and Debian

Enable the module. If it is already enabled, the command says so and changes nothing:

sudo a2enmod deflate

Create a configuration file for the types you want to compress:

sudo nano /etc/apache2/conf-available/compression.conf
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
    AddOutputFilterByType DEFLATE text/javascript application/javascript application/json
    AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/rss+xml
    AddOutputFilterByType DEFLATE image/svg+xml
    DeflateCompressionLevel 6
</IfModule>

Enable the file, check the syntax and reload Apache:

sudo a2enconf compression
sudo apachectl configtest
sudo systemctl reload apache2

AlmaLinux, Rocky Linux and CentOS Stream 9

Check that the module is loaded:

sudo httpd -M | grep deflate

Put the same <IfModule mod_deflate.c> block shown above into a new file, then test and reload:

sudo nano /etc/httpd/conf.d/compression.conf
sudo apachectl configtest
sudo systemctl reload httpd

Using .htaccess instead

On shared hosting, or wherever you cannot edit the server configuration, the AddOutputFilterByType lines also work in a .htaccess file in the site's document root, provided the module is loaded and the server allows overrides there. DeflateCompressionLevel is only valid in the server or virtual host configuration, so leave that line out of .htaccess.

mod_deflate adds a Vary: Accept-Encoding header to compressed responses by itself, so caches and CDNs keep compressed and uncompressed copies apart.

Enable gzip in Nginx

Nginx has gzip support built in, but its defaults are conservative: compression is off unless a configuration file turns it on, and once it is on only text/html is compressed, at level 1. The nginx.conf shipped by Ubuntu and Debian contains gzip on; followed by several commented-out settings, which means CSS, JavaScript and JSON are still sent uncompressed until you list their types.

Open the main configuration file:

sudo nano /etc/nginx/nginx.conf

Inside the http { } block, make the gzip section look like this. Edit the existing lines rather than adding a second gzip on;, because Nginx refuses to start with duplicate directives in the same block:

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml application/rss+xml image/svg+xml;

What the directives do:

  • gzip_vary on adds Vary: Accept-Encoding, so proxies do not hand a compressed copy to a client that cannot read it.
  • gzip_proxied any also compresses responses to requests that arrive through a proxy or CDN (requests with a Via header). The default is off.
  • gzip_comp_level accepts 1 to 9. Levels around 4 to 6 give most of the size reduction; higher levels cost noticeably more CPU for a few percent.
  • gzip_min_length 1024 skips responses smaller than about 1 KB, where compression saves nothing. The length is taken from the Content-Length header.
  • gzip_types lists the MIME types to compress in addition to text/html, which is always included and must not be listed again.

Test the configuration and reload:

sudo nginx -t
sudo systemctl reload nginx

Older configurations often carry gzip_disable "msie6";. It only excludes Internet Explorer 4 to 6 and does no harm, but you no longer need it.

Verify that compression works

Request a text file and ask for gzip explicitly:

curl -s -o /dev/null -D - -H "Accept-Encoding: gzip" https://example.com/style.css

A compressed response contains these headers:

content-encoding: gzip
vary: Accept-Encoding

To see the saving, compare the number of bytes transferred with and without compression:

curl -s -o /dev/null -w "%{size_download}\n" https://example.com/style.css
curl -s -o /dev/null -w "%{size_download}\n" -H "Accept-Encoding: gzip" https://example.com/style.css

If the header is missing, check that the file is larger than the minimum length, that its Content-Type is in your list, and that you reloaded the server.

Add Brotli alongside gzip

Brotli is a newer compression format that browsers accept over HTTPS and that usually beats gzip on text. Keep gzip enabled as the fallback.

Apache 2.4.26 and later include mod_brotli. On Ubuntu and Debian:

sudo a2enmod brotli

Add this line to /etc/apache2/conf-available/compression.conf, outside the mod_deflate block, then reload Apache:

AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml text/javascript application/javascript application/json image/svg+xml

For Nginx, Brotli is a third-party module that Debian 12 and Ubuntu 24.04 package separately (on Ubuntu it is in the universe component):

sudo apt install libnginx-mod-http-brotli-filter
sudo nano /etc/nginx/conf.d/brotli.conf
brotli on;
brotli_comp_level 5;
brotli_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml image/svg+xml;
sudo nginx -t
sudo systemctl reload nginx

Test it with -H "Accept-Encoding: br" in the curl command above; the response should show content-encoding: br.

Frequently asked questions

Is compression safe to use with HTTPS?

For static files, yes. The Apache and Nginx documentation both warn about the BREACH family of attacks, which can affect pages that are compressed, served over TLS and reflect user input on the same page as a secret such as a CSRF token. If your application has such pages, protect the tokens in the application or exclude those responses from compression.

Why are my JavaScript files still not compressed?

The MIME type the server sends has to match an entry in your list exactly. Depending on the server version, .js files are sent as application/javascript or text/javascript, so list both. Check the content-type header of the response to see which one you get.

Does compression slow the server down?

It uses some CPU for every compressed response, which is why moderate levels are recommended. For files that never change you can compress once in advance: Nginx can serve ready-made .gz files through gzip_static when it is built with the ngx_http_gzip_static_module, and the mod_deflate documentation describes an equivalent setup for Apache.

I use a CDN. Do I still need this?

Yes. Compressing at the origin reduces the traffic between your server and the CDN, and gzip_proxied any together with the Vary header makes sure the CDN receives and stores the right variant.

Share story:

Continue reading other guides on How VPS.