GZIP and Brotli Compression: Complete Guide for Apache and Nginx — Reduce Your File Size by 70-90%
Tutorials

GZIP and Brotli Compression: Complete Guide for Apache and Nginx — Reduce Your File Size by 70-90%

Learn how to enable GZIP and Brotli compression on your Apache and Nginx servers. Step-by-step tutorial for all levels, with ready-to-use configurations and verification tools.

28.11.2025
12 min read
Share this article:
gzip
brotli
apache
nginx
compression
performance
serveur
optimisation

Why enable server compression?

Server compression is one of the most effective and simplest optimizations to implement. Unlike minification which optimizes source code, compression reduces file size during transfer between server and browser. Combined with minification, it can reduce your total file size by 70 to 90%, drastically improving load times and user experience.

70-90% reduction in transferred file sizes
50-70% improvement in load times
Substantial savings on bandwidth and hosting costs
Better score in Google's Core Web Vitals
Optimized user experience, especially on slow connections

Real-world impact of compression on performance

Concrete performance metrics

Here are real data from an e-commerce site that enabled GZIP compression:

Chart showing performance improvement after enabling compression

Before optimization

File size:2.5 MB
Load time:1.2s

After optimization

File size:380 KB
Load time:0.3s

Improvements

85% reduction in transferred size (from 2.5 MB to 380 KB)
75% improvement in load time (from 1.2s to 0.3s)
85% savings on bandwidth
Significant improvement in LCP (Largest Contentful Paint)
Impact on SEO and Core Web Vitals

Compression directly improves your web performance metrics:

LCP (Largest Contentful Paint): -40 to -60% thanks to smaller files
FID (First Input Delay): -30% thanks to less data to parse
CLS (Cumulative Layout Shift): Reduced reflows
TTI (Time to Interactive): -50% waiting time

What is server compression?

Compression vs Minification: Understanding the difference

Minification and compression are two complementary but different techniques. Minification removes spaces, comments and optimizes source code. Compression (GZIP or Brotli) then compresses the already minified file for additional gain during network transfer.

1
1. Write your JavaScript or CSS code normally
2
2. Minify the code (40-80% reduction)
3
3. Compress the minified file with GZIP/Brotli (additional 60-80% reduction)
4
4. Result: final file 5 to 10 times smaller than the original
GZIP vs Brotli: Which compression to choose?

GZIP is the most widespread compression and compatible with all browsers. Brotli is newer and offers better compression (15-20% better than GZIP), but requires additional configuration.

GZIP

compression:60-80% reduction
compatibility:100% of browsers
cpu:Low CPU consumption
setup:Simple configuration

Brotli

compression:70-90% reduction
compatibility:All modern browsers (2016+)
cpu:Slightly higher CPU consumption
setup:More complex configuration

GZIP Configuration on Apache

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

The simplest way to enable GZIP on Apache is to use the .htaccess file. This method works even if you don't have access to the main server configuration.

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 AddOutputFilterByType DEFLATE font/woff AddOutputFilterByType DEFLATE font/woff2 # 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 more global and performant configuration, configure GZIP directly in Apache's main configuration file.

Configuration

# In /etc/apache2/apache2.conf or /etc/httpd/httpd.conf # Enable deflate module 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 font/woff font/woff2 # Exclude already compressed files SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|webp|ico|gz|zip|bz2|rar)$ no-gzip # Exclude files that are too small (less than 1 KB) DeflateFilterByType text/html text/plain text/xml text/css text/javascript application/javascript </IfModule>

Brotli Configuration on Apache

Installing mod_brotli module

Brotli requires installing an additional module. Here's how to install it depending on your system.

Installation on Ubuntu/Debian

# Install dependencies sudo apt-get update sudo apt-get install -y libbrotli-dev # Install mod_brotli from sources # Download from: https://github.com/kjdev/apache-mod-brotli # Or use PPA if available

Enable the module

# Enable the module sudo a2enmod brotli sudo systemctl restart apache2
Brotli configuration in .htaccess

Once the module is installed, configure Brotli in your .htaccess with priority over GZIP.

Configuration

# Brotli configuration (priority over GZIP) <IfModule mod_brotli.c> # Compression level (1-11, 6 is recommended) BrotliCompressionLevel 6 # 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 font/woff font/woff2 # 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>

GZIP Configuration on Nginx

Basic configuration in nginx.conf

Nginx includes GZIP support by default. You just need to enable it in your configuration.

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/xml+rss application/atom+xml image/svg+xml font/ttf font/otf font/woff font/woff2; # Minimum file size to compress (1 KB) gzip_min_length 1000; # Don't compress already compressed files gzip_disable "msie6";
Optimal 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/xml+rss application/atom+xml image/svg+xml font/ttf font/otf font/woff font/woff2; gzip_min_length 1000; gzip_disable "msie6"; # Cache compressed files gzip_static on;

Brotli Configuration on Nginx

Installing ngx_brotli module

Nginx requires installing the ngx_brotli module. Here's how to proceed.

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 fallback.

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/xml+rss application/atom+xml image/svg+xml font/ttf font/otf font/woff font/woff2; 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;

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.

Compression verification tool interface showing results

GiftOfSpeed Gzip/Brotli Test

Free and simple tool to verify GZIP and Brotli compression of your website

https://www.giftofspeed.com/gzip-test/
Features:
  • Instant GZIP compression verification
  • Brotli compression detection
  • Display of original vs compressed size
  • Reduction percentage displayed
  • Test on multiple resources (HTML, CSS, JS)
How to use:
1
1. Visit https://www.giftofspeed.com/gzip-test/
2
2. Enter your website URL
3
3. Click 'Test webpage'
4
4. Review the results: you'll see if GZIP/Brotli is active and the reduction percentage
Manual verification with browser DevTools

You can also verify compression directly in your browser's development tools.

Open DevTools

Press F12 or Cmd+Option+I (Mac) / Ctrl+Shift+I (Windows/Linux)

Network tab

Go to the 'Network' tab and reload the page

Check headers

Click on a resource (HTML, CSS, JS) and look at the 'Headers' tab. You should see 'Content-Encoding: gzip' or 'Content-Encoding: br' (Brotli)

Example

# Example response header with GZIP compression HTTP/1.1 200 OK Content-Type: text/css Content-Encoding: gzip Content-Length: 15234 # Example response header with Brotli compression HTTP/1.1 200 OK Content-Type: text/css Content-Encoding: br 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 (TTF, OTF, WOFF, WOFF2)
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)
Optimal compression levels

Find the right balance between size and CPU consumption.

GZIP level 6: Good compromise for most sites
Brotli level 6: Optimal for Brotli (better compression than GZIP level 6)
Avoid level 9: Marginal gain for significant CPU consumption
Level 1-3: For servers with limited CPU resources
Minimum file size

Don't compress very small files, the gain is negligible.

Cache and compression

Combine compression and caching for optimal performance.

Serve pre-compressed files (.gz, .br) when possible
Configure appropriate Cache-Control headers
Use ETags for cache validation
Enable compressed file caching in Nginx (gzip_static)

Common troubleshooting

Compression is not working

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

Verify that mod_deflate (Apache) or gzip (Nginx) module is enabled
Check server error logs for error messages
Make sure MIME types are correctly configured
Verify that the browser sends the 'Accept-Encoding: gzip, br' header
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 compression on your Apache or Nginx servers is an essential optimization that can drastically reduce the size of your transferred files. Combined with minifying your JavaScript and CSS code, compression can significantly improve your website performance, reduce your bandwidth costs and improve your Google ranking thanks to better Core Web Vitals.

Enable GZIP first (compatible with all browsers)
Add Brotli if possible for even better compression
Configure a minimum size of 1 KB to avoid compressing small files
Test regularly with GiftOfSpeed Gzip/Brotli Test to verify everything works
Combine compression and minification for optimal results
Monitor impact on your Core Web Vitals in Google Search Console

Ready to optimize your files?

Try our minification tool now
Share this article
Share this article:
GZIP and Brotli Compression: Complete Guide for Apache and Nginx — Reduce Your File Size by 70-90%