How to Fix Apache Gzip Compression Not Working
In this tutorial, you'll learn about How to Fix Apache Gzip Compression Not Working. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Apache responses are not compressed even though mod_deflate is enabled — the browser shows uncompressed content sizes and the Content-Encoding: gzip header is missing from responses.
The Problem
$ curl -H "Accept-Encoding: gzip" -I http://example.com/style.css
HTTP/1.1 200 OK
Content-Type: text/css
# Missing: Content-Encoding: gzip
Step-by-Step Fix
Step 1: Enable mod_deflate
sudo a2enmod deflate
sudo systemctl restart apache2
Step 2: Configure 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/json
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE image/svg+xml
# Remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
Step 3: Set compression level
<IfModule mod_deflate.c>
DeflateCompressionLevel 6
</IfModule>
Step 4: Test compression
curl -H "Accept-Encoding: gzip" -I http://example.com/style.css
Expected:
HTTP/1.1 200 OK
Content-Encoding: gzip
Step 5: Verify with size comparison
# Uncompressed size
curl -s http://example.com/style.css | wc -c
# Compressed size
curl -s -H "Accept-Encoding: gzip" http://example.com/style.css | wc -c
Prevention Tips
- Set
DeflateCompressionLevel 6for best speed/size balance - Do not compress already compressed formats (JPEG, PNG, GIF, WebP)
- Use Brotli (
mod_brotli) alongside gzip for better compression - Test compression ratios with GTmetrix or PageSpeed Insights
Common Mistakes with compression gzip
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world APACHE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro