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. curlon 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 deflateCreate 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 apache2AlmaLinux, Rocky Linux and CentOS Stream 9
Check that the module is loaded:
sudo httpd -M | grep deflatePut 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 httpdUsing .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.confInside 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 onaddsVary: Accept-Encoding, so proxies do not hand a compressed copy to a client that cannot read it.gzip_proxied anyalso compresses responses to requests that arrive through a proxy or CDN (requests with aViaheader). The default isoff.gzip_comp_levelaccepts 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 1024skips responses smaller than about 1 KB, where compression saves nothing. The length is taken from theContent-Lengthheader.gzip_typeslists the MIME types to compress in addition totext/html, which is always included and must not be listed again.
Test the configuration and reload:
sudo nginx -t
sudo systemctl reload nginxOlder 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.cssA compressed response contains these headers:
content-encoding: gzip
vary: Accept-EncodingTo 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.cssIf 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 brotliAdd 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+xmlFor 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-filtersudo nano /etc/nginx/conf.d/brotli.confbrotli 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 nginxTest 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.