
GZIP and Brotli Compression: Complete Guide for Apache and Nginx
Enable GZIP and Brotli on Apache (mod_deflate) and Nginx (ngx_brotli). Copy-paste configs, set MIME types, and verify with DevTools or online checkers.
Why enable server compression?
Server compression is one of the simplest optimizations to set up and one of the most effective. Minification rewrites the source code. Compression shrinks files while they travel from the server to the browser. Together, they often reduce the transferred size of text files by 70 to 90%, which shortens load times.
Real-world impact of compression on performance
An illustrative example, with orders of magnitude typical of a text-heavy page before and after enabling GZIP:

Before optimization
- File size
- 2.5 MB
- Load time
- 1.2s
After optimization
- File size
- 380 KB
- Load time
- 0.3s
Improvements
Compression mainly affects loading metrics:
What is server compression?
Minification and compression are two different techniques that work together. Minification removes spaces and comments and shortens the source code. Compression (GZIP or Brotli) then encodes the already minified file to shrink it further during network transfer.
GZIP is the most widely used encoding, and every browser supports it. Brotli compresses further but may need an extra server module. We tested it on production JavaScript and CSS files. At level 6, Brotli produced files 3 to 11% smaller than GZIP at level 6, and 10 to 22% smaller at level 11.
GZIP
Brotli
GZIP Configuration on Apache
The simplest way to enable GZIP on Apache is the .htaccess file. It works without access to the main server configuration, as long as mod_deflate is loaded and the host allows these directives (AllowOverride FileInfo).
Create or modify the .htaccess file
Verify that mod_deflate module is enabled
For a server-wide setup, configure GZIP directly in Apache's main configuration file. Apache reads it once at startup, instead of checking .htaccess files on every request.
Configuration
apachectl configtest, then restart Apache with sudo systemctl restart apache2 (the service is called httpd on some distributions).Brotli Configuration on Apache
Since version 2.4.26, Apache ships with its own mod_brotli module. On Debian and Ubuntu it is part of the apache2 package: check your Apache version, then enable the module. On other systems, load it with a LoadModule line in httpd.conf.
Check the module on Ubuntu/Debian
Enable the module
Once the module is enabled, set up Brotli alongside GZIP in the .htaccess file. Apache then serves Brotli to browsers that accept it and GZIP to the others. Only the compression level stays in httpd.conf, because .htaccess does not accept it.
Configuration
Accept-Encoding header and only adds br over HTTPS. Apache then picks the encoding. The Content-Encoding response header shows whether Brotli or GZIP was served.GZIP Configuration on Nginx
Nginx's gzip module is built in by default, but compression stays off until you turn it on in your configuration. Without further settings, only text/html responses are compressed, at level 1.
Configuration
sudo nginx -t, then restart Nginx with sudo systemctl restart nginx.Advanced configuration to maximize performance in production.
Configuration
gzip_static serves the pre-compressed .gz file stored next to the original when it exists, so Nginx does not compress it on every request. The module is not built by default. Check that the output of nginx -V contains --with-http_gzip_static_module.Brotli Configuration on Nginx
Nginx does not include Brotli. You need ngx_brotli, a module published by Google. Compile it against the source of your exact Nginx version, or install a package if your distribution offers one (libnginx-mod-http-brotli-filter on Debian 12 and later).
Installation from sources
Installation via package (if available)
Once the module is installed, configure Brotli with GZIP as a fallback. If you built it as a dynamic module, load it first with load_module at the top of nginx.conf, outside the http block.
Configuration
br in its Accept-Encoding header, which current browsers only do over HTTPS. Other requests get GZIP.Verify that compression is working
The simplest way to verify if GZIP or Brotli compression is active on your site is to use a specialized online tool.

GiftOfSpeed Gzip/Brotli Test
Free and simple tool to verify GZIP and Brotli compression of your website
https://www.giftofspeed.com/gzip-test/Features:
- •Checks whether GZIP is enabled
- •Detects Brotli compression
- •Shows the original size of the tested page
- •Shows the savings compression brings
- •Tests one URL at a time
How to use:
You can also verify compression directly in your browser's development tools.
Open DevTools
Press F12. On macOS, you can also press Cmd+Option+I, and on Windows or Linux, Ctrl+Shift+I.
Network tab
Open the Network tab and reload the page
Check headers
Select a resource (HTML, CSS, JS) and open its Headers tab. The response should contain Content-Encoding: gzip or Content-Encoding: br (Brotli), along with Vary: Accept-Encoding.
Example
Best practices and recommendations
Compress text files, but avoid compressing already compressed files.
Compress:
Don't compress:
.gz, .bz2)Find the right balance between size and CPU consumption.
Very small files are not worth compressing: the gain is negligible.
gzip_min_length and brotli_min_length (20 bytes by default). Apache's mod_deflate has no equivalent setting.Compression and caching add up: fewer bytes per request, and fewer requests.
.gz, .br) when you can. You compress them once, even at Brotli level 11, which is too slow on the fly.Cache-Control headers.gz files with gzip_static and .br files with brotli_static (ngx_brotli module)Common troubleshooting
If compression doesn't seem active, check these points:
gzip on; is set on NginxAccept-Encoding header with gzip or br (browsers only send br over HTTPS)Common problems with Apache and their solutions.
Errors and solutions
Common problems with Nginx and their solutions.
Errors and solutions
Conclusion
Enabling GZIP and Brotli on Apache or Nginx greatly reduces the size of the files you send. Combined with minified JavaScript and CSS, compression speeds up your pages and uses less bandwidth. It can also improve LCP, one of the Core Web Vitals that Google takes into account.
Ready to optimize your files?
Related Articles

Paste a token and a JWKS to check an RS256, ES256 or HS256 signature in your browser. How kid picks the key, measured errors and the claims to check.

HTTP/2 and HTTP/3 change how files travel, not how big they are. Measured sizes, compile times and bundling tests show what minification still saves.

Convert a Unix epoch to a readable date and back, tell seconds from milliseconds, and avoid time zone, JWT and clock change traps, all in your browser.