Era Host hosting
EraHost – Free Domain, Cheap Hosting!
Client Area
Support 24/7
Menu

Gzip and Vary: Accept-Encoding Header

4 min read
24.12.2025

When using gzip compression on your server, it is essential to include the Vary: Accept-Encoding header in HTTP responses. This header ensures that caching mechanisms (like CDNs, proxies, and browsers) differentiate between compressed and uncompressed versions of a resource.

Gzip Vary Accept-Encoding Headers
Vary: Accept-Encoding — tells caches "different responses by client capability."

For closely related caching / performance topics, see Enabling and Configuring mod_expires in Apache and AddDefaultCharset Apache Guide.

Why Add the Vary: Accept-Encoding Header with Gzip?

  1. Caching Compatibility: Some clients may not support gzip compression. The Vary: Accept-Encoding header ensures that caches store and serve the correct version of a resource based on whether the client supports gzip.
  2. Prevent Errors: Without the Vary header, a cache might serve a gzipped resource to a client that does not support gzip, causing unreadable or corrupted content.
  3. Improve Performance: For clients that support gzip, cached compressed resources are smaller and load faster, improving user experience.
  4. SEO Compliance: Search engines like Google recommend using Vary: Accept-Encoding to avoid serving incorrect versions of content to crawlers.

How to Enable Gzip and the Vary: Accept-Encoding Header in Apache

Enable Required Modules

Ensure mod_deflate and mod_headers are enabled:

sudo a2enmod deflate
sudo a2enmod headers
sudo systemctl restart apache2

Modify Configuration

Add the following to your .htaccess file or Apache configuration:

<IfModule mod_deflate.c>

    # Enable gzip compression

    AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json application/xml text/javascript



    # Add Vary: Accept-Encoding header

    <IfModule mod_headers.c>

        Header append Vary: Accept-Encoding

    </IfModule>

</IfModule>

Restart Apache

sudo systemctl restart apache2

How to Enable Gzip and the Vary: Accept-Encoding Header in Nginx

Open Nginx Configuration

Open your Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

Add Gzip Configuration

gzip on;

gzip_vary on;

gzip_proxied any;

gzip_comp_level 5;

gzip_types text/plain text/css application/json application/javascript text/xml application/xml+rss text/javascript;

gzip_disable "msie6";
  • gzip on;: Enables gzip compression.
  • gzip_vary on;: Automatically adds Vary: Accept-Encoding to the response header.

Reload Nginx

sudo systemctl reload nginx
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

How to Enable Gzip and the Vary: Accept-Encoding Header in cPanel

Log in to cPanel

Navigate to your cPanel dashboard.

Enable Compression

Go to Optimize Website under the Software section. Select Compress All Content or specify MIME types. Save the settings.

Edit .htaccess (Optional)

Add the Vary: Accept-Encoding header manually in the .htaccess file:

<IfModule mod_headers.c>

    Header append Vary: Accept-Encoding

</IfModule>

How to Verify Gzip and Vary: Accept-Encoding

  1. Using cURL: Check if gzip is enabled and Vary is set:
  2. curl -I -H "Accept-Encoding: gzip" https://yourdomain.com

    Look for:

    Content-Encoding: gzip
    
    Vary: Accept-Encoding
  3. Online Tools: Use tools like:
  4. Browser Developer Tools: Open your browser Developer Tools (F12). Go to the Network tab, reload the page, and inspect the response headers.

Best Practices

  1. Enable Compression for Key MIME Types: Compress text-based resources (HTML, CSS, JS, JSON, XML). Avoid compressing binary files (e.g., images, videos) as they are already compressed.
  2. Monitor Performance: Regularly test your site performance and ensure compression is functioning.
  3. Test Across Clients: Verify that all clients (modern and legacy) receive compatible versions of your resources.

By enabling gzip and adding the Vary: Accept-Encoding header, you improve caching efficiency, ensure compatibility, and enhance website performance.

Frequently asked questions
Downstream cache serves the cached representation to all clients regardless of their Accept-Encoding. If the cached version is gzipped and a (rare) client sent `Accept-Encoding: identity`, they get gzipped bytes they can't decode. Modern clients almost always support gzip, so visible breakage is rare — but RFC compliance requires Vary, and some CDNs refuse to cache without it.
Slightly. With `Vary: Accept-Encoding`, the cache stores separate entries for `gzip`, `br`, `deflate`, `identity` — up to 4x storage per resource. For most sites, fine. For very high-cardinality caches (CDN edge), some providers normalize Accept-Encoding before caching. Cloudflare does this automatically. Self-hosted Varnish: explicitly normalize in vcl_recv.
Yes — Vary: Accept-Encoding covers all encodings including br. The client signals capability via `Accept-Encoding: br, gzip, deflate` and the server picks one. Same Vary header tells caches to differentiate. If you support both gzip and brotli, set up both in the web server and Vary handles the rest.
Cloudflare gzip-compresses outbound to clients regardless of your origin. Origin-to-Cloudflare can be uncompressed; CF re-compresses for client. But Cloudflare also caches origin responses — having origin send gzipped + Vary lets CF cache the compressed bytes, saving CPU on every cache hit. Recommended: keep origin gzip enabled with Vary.
Related articles
Specify a Vary: Accept-Encoding Header
How to Add Vary: Accept Header Using cPanel
How to Specify a Vary: Accept-Encoding Header in Apache