GZIP and Brotli Compression: Complete Guide for Apache and Nginx

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.

28.11.2025
12 min read
Share this article:
GZIPBrotliApacheNginxCompressionPerformanceServerOptimizationTutorial

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.

60 to 80% less data to transfer for HTML, CSS and JavaScript
Shorter download times for every text resource
Less bandwidth used, which can lower hosting costs
Can improve LCP, one of Google's Core Web Vitals
A noticeable difference on slow connections

Real-world impact of compression on performance

Concrete performance metrics

An illustrative example, with orders of magnitude typical of a text-heavy page before and after enabling GZIP:

Illustration of file size and load time before and after compression

Before optimization

File size
2.5 MB
Load time
1.2s

After optimization

File size
380 KB
Load time
0.3s

Improvements

85% less data transferred (from 2.5 MB to 380 KB)
Load time down 75% (from 1.2 s to 0.3 s)
85% less bandwidth
A faster LCP (Largest Contentful Paint) when it depends on HTML, CSS or JS
Impact on SEO and Core Web Vitals

Compression mainly affects loading metrics:

LCP (Largest Contentful Paint): smaller HTML, CSS and JS files arrive sooner, so the main content can render earlier
FID (First Input Delay): replaced by INP as a Core Web Vital in 2024. Compression does not reduce the JavaScript the browser has to parse, since it decompresses the full file.
CLS (Cumulative Layout Shift): no direct effect. Layout shifts depend on how the page is built, not on file size.
TTI (Time to Interactive): no longer counted in the Lighthouse score since version 10. Scripts that download faster still shorten the wait.

What is server compression?

Compression vs Minification: Understanding the difference

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.

Think of your file as a suitcase. Minification takes out everything you don't need. Compression vacuum-packs what's left so that the suitcase takes up even less space during the trip. Using both gives you the smallest file.
1
Write your JavaScript or CSS code as usual
2
Minify the code (often 40 to 80% smaller)
3
Compress the minified file with GZIP or Brotli (another 60 to 80% off)
4
Result: the file sent is often 5 to 10 times smaller than the original
GZIP vs Brotli: Which compression to choose?

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

Compression:60-80% reduction
Compatibility:100% of browsers
CPU cost:Low
Setup:Simple

Brotli

Compression:65-85% reduction
Compatibility:All current browsers, over HTTPS only
CPU cost:Higher at high levels
Setup:Extra module on Nginx
Recommendation: enable both. Serve Brotli to browsers that accept it and keep GZIP for the others.

GZIP Configuration on Apache

Method 1: Enable via .htaccess (recommended for beginners)

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

# Enable GZIP compression <IfModule mod_deflate.c> # Compress HTML, CSS, JavaScript, Text, XML and fonts AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/json AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/atom+xml AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE font/otf # Don't compress already compressed images SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|webp)$ no-gzip dont-vary SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary </IfModule>

Verify that mod_deflate module is enabled

# To verify, run this command on your server: apache2ctl -M | grep deflate # If the module is not listed, enable it with: sudo a2enmod deflate sudo systemctl restart apache2
Method 2: Configuration in httpd.conf (for advanced users)

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

# In /etc/apache2/apache2.conf or /etc/httpd/httpd.conf # Load the deflate module # Debian/Ubuntu: run sudo a2enmod deflate instead of this line LoadModule deflate_module modules/mod_deflate.so # Compression configuration <IfModule mod_deflate.c> # Compression level (1-9, 6 is a good compromise) DeflateCompressionLevel 6 # MIME types to compress AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css AddOutputFilterByType DEFLATE text/javascript application/javascript AddOutputFilterByType DEFLATE application/json application/xml AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE font/ttf font/otf # Exclude already compressed files SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|webp|ico|gz|zip|bz2|rar)$ no-gzip # mod_deflate has no minimum size setting </IfModule>
Check the file first with apachectl configtest, then restart Apache with sudo systemctl restart apache2 (the service is called httpd on some distributions).

Brotli Configuration on Apache

Installing mod_brotli module

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

# mod_brotli ships with Apache 2.4.26 and later: check your version apache2ctl -v # On Debian and Ubuntu, the module is part of the apache2 package ls /usr/lib/apache2/modules/mod_brotli.so

Enable the module

# Debian/Ubuntu: enable the module sudo a2enmod brotli sudo systemctl restart apache2 # Other systems: load the module in httpd.conf LoadModule brotli_module modules/mod_brotli.so
Brotli configuration in .htaccess

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

# Brotli configuration (priority over GZIP) <IfModule mod_brotli.c> # The level (BrotliCompressionQuality, 0-11, default 5) is set # in httpd.conf or a virtual host: .htaccess does not accept it # MIME types to compress AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css AddOutputFilterByType BROTLI_COMPRESS text/javascript application/javascript AddOutputFilterByType BROTLI_COMPRESS application/json application/xml AddOutputFilterByType BROTLI_COMPRESS application/xhtml+xml application/rss+xml AddOutputFilterByType BROTLI_COMPRESS image/svg+xml AddOutputFilterByType BROTLI_COMPRESS font/ttf font/otf # Exclude already compressed files SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|webp|ico|gz|zip|bz2|rar)$ no-brotli </IfModule> # GZIP configuration as fallback <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/html text/css text/javascript application/javascript application/json </IfModule>
The browser lists the encodings it accepts in its 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

Basic configuration in nginx.conf

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

# In /etc/nginx/nginx.conf (global configuration) # Or in your specific site file # Enable GZIP compression gzip on; gzip_vary on; gzip_proxied any; # Compression level (1-9, 6 is recommended) gzip_comp_level 6; # MIME types to compress gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/rss+xml application/atom+xml image/svg+xml font/ttf font/otf; # Minimum file size to compress (1 KB) gzip_min_length 1000; # Disable gzip for Internet Explorer 4 to 6 gzip_disable "msie6";
After editing, test the configuration with sudo nginx -t, then restart Nginx with sudo systemctl restart nginx.
Recommended configuration for production

Advanced configuration to maximize performance in production.

Configuration

# Optimal GZIP configuration for Nginx gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/rss+xml application/atom+xml image/svg+xml font/ttf font/otf; gzip_min_length 1000; gzip_disable "msie6"; # Serve pre-compressed .gz files when they exist gzip_static on;
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

Installing ngx_brotli module

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

# Install dependencies sudo apt-get update sudo apt-get install -y libbrotli-dev # Download ngx_brotli cd /tmp git clone https://github.com/google/ngx_brotli.git cd ngx_brotli git submodule update --init # Recompile Nginx with the module (requires Nginx sources) # Consult the official documentation for complete details

Installation via package (if available)

# On some distributions, ngx_brotli is available via package # Check with your package manager
Brotli configuration in nginx.conf

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

# Brotli configuration (priority over GZIP) brotli on; brotli_comp_level 6; brotli_types text/plain text/css text/xml text/javascript application/json application/javascript application/rss+xml application/atom+xml image/svg+xml font/ttf font/otf; brotli_min_length 1000; # GZIP configuration as fallback gzip on; gzip_vary on; gzip_comp_level 6; gzip_types text/plain text/css text/javascript application/javascript application/json; gzip_min_length 1000;
Nginx answers in Brotli when the browser lists br in its Accept-Encoding header, which current browsers only do over HTTPS. Other requests get GZIP.

Verify that compression is working

Use an online verification tool

The simplest way to verify if GZIP or Brotli compression is active on your site is to use a specialized online tool.

Illustration of compression checks with online tools and browser DevTools

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:
1
Open https://www.giftofspeed.com/gzip-test/
2
Enter your website URL
3
Select “Test webpage”
4
Read the results: they show whether GZIP or Brotli is active and how much it saves
Manual verification with browser DevTools

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

# Example response header with GZIP compression HTTP/1.1 200 OK Content-Type: text/css Content-Encoding: gzip Vary: Accept-Encoding Content-Length: 15234 # Example response header with Brotli compression HTTP/1.1 200 OK Content-Type: text/css Content-Encoding: br Vary: Accept-Encoding Content-Length: 12345

Best practices and recommendations

Which files to compress?

Compress text files, but avoid compressing already compressed files.

Compress:

HTML, CSS, JavaScript
JSON, XML, SVG
Fonts in TTF or OTF (WOFF and WOFF2 are already compressed)
Text files (TXT, CSV)

Don't compress:

Already compressed images (JPEG, PNG, GIF, WebP)
Binary files (PDF, ZIP, EXE)
Video and audio files
Already compressed files (.gz, .bz2)
Choosing a compression level

Find the right balance between size and CPU consumption.

GZIP level 6: Good compromise for most sites
Brotli level 6: about as fast as GZIP level 6 in our tests, with files a few percent smaller
Avoid GZIP level 9: in our tests, files at most 1% smaller for about four times the CPU time
Levels 1 to 3: for servers with limited CPU
Minimum file size

Very small files are not worth compressing: the gain is negligible.

On Nginx, set a threshold of about 1 KB with gzip_min_length and brotli_min_length (20 bytes by default). Apache's mod_deflate has no equivalent setting.
Cache and compression

Compression and caching add up: fewer bytes per request, and fewer requests.

Serve pre-compressed files (.gz, .br) when you can. You compress them once, even at Brotli level 11, which is too slow on the fly.
Set suitable Cache-Control headers
Use ETags for cache validation
On Nginx, serve .gz files with gzip_static and .br files with brotli_static (ngx_brotli module)

Common troubleshooting

Compression is not working

If compression doesn't seem active, check these points:

Check that mod_deflate is loaded on Apache, or that gzip on; is set on Nginx
Check server error logs for error messages
Make sure the MIME type of your files is in the list of types to compress
Check that the browser sends an Accept-Encoding header with gzip or br (browsers only send br over HTTPS)
Test with an online tool like GiftOfSpeed Gzip/Brotli Test
Apache configuration errors

Common problems with Apache and their solutions.

Errors and solutions

# Error: 'mod_deflate' not found # Solution: Enable the module sudo a2enmod deflate sudo systemctl restart apache2 # Error: Syntax error in .htaccess # Solution: Check the syntax, each line must be correct
Nginx configuration errors

Common problems with Nginx and their solutions.

Errors and solutions

# Error: 'unknown directive "gzip"' # Solution: Check that you don't have a typo # gzip must be written exactly like this # Error: Configuration test failed # Solution: Test with sudo nginx -t to see exact errors

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?

Enable GZIP first (compatible with all browsers)
Add Brotli where you can, for even smaller files
On Nginx, set a minimum size of about 1 KB so that small files stay uncompressed
Test regularly with GiftOfSpeed Gzip/Brotli Test to verify everything works
Combine minification and compression: the gains add up
Monitor impact on your Core Web Vitals in Google Search Console
Share this article
Share this article: