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.
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.
Real-world impact of compression on performance
Here are real data from an e-commerce site that enabled GZIP compression:
Before optimization
After optimization
Improvements
Compression directly improves your web performance metrics:
What is server compression?
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.
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
Brotli
GZIP Configuration on Apache
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 apache2For 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
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 availableEnable the module
# Enable the module
sudo a2enmod brotli
sudo systemctl restart apache2Once 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
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";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
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 detailsInstallation via package (if available)
# On some distributions, ngx_brotli is available via package
# Check with your package managerOnce 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
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:
- •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:
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: 12345Best practices and recommendations
Compress text files, but avoid compressing already compressed files.
Compress:
Don't compress:
Find the right balance between size and CPU consumption.
Don't compress very small files, the gain is negligible.
Combine compression and caching for optimal performance.
Common troubleshooting
If compression doesn't seem active, check these points:
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 correctCommon 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 errorsConclusion
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.
Ready to optimize your files?
Try our minification tool nowRelated Articles
Learn how JavaScript and CSS minification can significantly improve your Core Web Vitals (LCP, FID, CLS) and boost your Google ranking in 2025.

Master CSS minification with CSSO and PurifyCSS. Advanced techniques, Webpack integration and performance optimization for ultra-fast websites.
Learn everything about JavaScript minification: techniques, tools, best practices and impact on web performance.