Skip to content

How to Fix Apache Gzip Compression Not Working

DodaTech Updated 2026-06-24 2 min read

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 6 for 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

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' 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

### Why is mod_deflate enabled but responses are not compressed?

The Accept-Encoding header must be present in the request. Browsers send this automatically, but curl requires -H "Accept-Encoding: gzip". Also check that the response Content-Type matches one of the AddOutputFilterByType directives.

Should I use mod_deflate or mod_brotli?

Use both with a preference for Brotli. Browsers that support Brotli (all modern browsers) get smaller files. Configure mod_brotli to fall back to mod_deflate for older browsers. Brotli typically achieves 20-30% better compression than gzip.

What compression level should I use for Apache?

Level 6 is the best balance for CPU usage and compression ratio. Levels 7-9 provide marginal gains (1-3%) with significantly higher CPU usage. Levels 1-5 compress faster but produce larger files. Stay at level 6 for most production workloads.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro