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

Vary: Accept-Encoding and Caching Compatibility

4 min read
17.05.2026

The Vary: Accept-Encoding HTTP response header is essential for ensuring that caches (such as browsers, CDNs, or reverse proxies) handle compressed and uncompressed versions of resources correctly. Without this header, caching mechanisms might serve the wrong version of a resource to clients, leading to errors or poor user experience.

Vary Accept-Encoding Caching
Vary: Accept-Encoding — separate cache entries per compression.

For the broader Vary: Accept-Encoding cluster, see Gzip and Vary: Accept-Encoding, cPanel Vary: Accept Header, How to Specify Vary: Accept-Encoding in Nginx, Vary: Accept-Encoding and Caching, and Vary: Accept-Encoding and Content Negotiation.

What Does Vary: Accept-Encoding Do for Caching?

When a server sends the Vary: Accept-Encoding header, it tells caching systems to differentiate cached responses based on the Accept-Encoding request header sent by the client. This ensures:

  1. Correct Resource Delivery: Compressed resources (e.g., gzip or Brotli) are served to clients that support them. Uncompressed resources are served to clients that do not support compression.
  2. Cache Segmentation: Separate versions of the resource are stored for each encoding type (compressed and uncompressed).
  3. Prevents Cache Mismatches: Ensures that clients only receive resources they can decode, avoiding errors like garbled content.

How Caching Systems Handle Vary: Accept-Encoding

Client Request

The client sends an HTTP request with the Accept-Encoding header indicating supported compression formats:

Accept-Encoding: gzip, br

Server Response

The server sends a response with the compressed resource and includes:

Content-Encoding: gzip

Vary: Accept-Encoding

Caching Behavior

  • The cache stores the compressed version of the resource but associates it specifically with requests that include Accept-Encoding: gzip, br.
  • If another client sends a request without Accept-Encoding or with a different value (e.g., Accept-Encoding: identity), the cache will fetch or store an uncompressed version of the resource.

Benefits of Using Vary: Accept-Encoding for Caching

Avoids Cache Corruption

Ensures that cached resources are compatible with the client capabilities. Prevents situations where compressed content is sent to a client that does not support it.

Improves Performance

Allows caching systems to serve precompressed resources directly, reducing the load on the server.

Better Compatibility

Guarantees that all clients, including older browsers and systems, receive resources they can handle.

SEO Best Practices

Ensures that search engine crawlers index the correct version of your resources, improving search engine rankings.

Risks of Not Using Vary: Accept-Encoding

If the Vary: Accept-Encoding header is not used:

  1. Cache Mismatches: Compressed resources may be served to clients that do not support them, resulting in unreadable content. Uncompressed resources may be served unnecessarily to clients that support compression, increasing bandwidth usage.
  2. Poor User Experience: Some users may experience errors or garbled content, especially on older browsers or systems with limited encoding support.
  3. Inconsistent Search Engine Indexing: Search engines may index the wrong version of the resource, potentially affecting SEO.
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

How to Add Vary: Accept-Encoding for Caching

In Apache

Enable mod_headers and add the following to your .htaccess or Apache configuration:

<IfModule mod_headers.c>

    Header append Vary: Accept-Encoding

</IfModule>

In Nginx

Add the following to your Nginx configuration file:

gzip on;

gzip_vary on;

gzip_types text/plain text/css application/json application/javascript;

Reload Nginx:

sudo systemctl reload nginx

In cPanel

  1. Log into cPanel.
  2. Go to File Manager and open the .htaccess file.
  3. Add:
  4. <IfModule mod_headers.c>
    
        Header append Vary: Accept-Encoding
    
    </IfModule>

In Cloudflare

Cloudflare automatically handles Vary: Accept-Encoding. Ensure compression is enabled in Speed > Optimization settings.

How to Verify Vary: Accept-Encoding

  1. Using cURL: Run the following command to check the headers:
  2. curl -I https://yourdomain.com

    Look for:

    Vary: Accept-Encoding
  3. Browser Developer Tools:
    • Open Developer Tools in your browser.
    • Go to the Network tab.
    • Click on a resource and check the headers.
  4. Online Tools: Use tools like WebPageTest or GTmetrix to confirm the presence of the Vary header.

Best Practices for Vary: Accept-Encoding

  1. Enable Compression: Use gzip or Brotli compression to reduce resource size.
  2. Avoid Overuse of Vary: Excessive use (e.g., Vary: Accept-Encoding, Accept-Language, User-Agent) can lead to cache fragmentation.
  3. Combine with Proper Cache-Control: Use Cache-Control headers to specify caching duration and behavior.
  4. Test Across Devices: Ensure that resources load correctly for all clients, including older browsers.
Frequently asked questions
Some CDNs see Vary as high-cardinality risk (many cache entries per URL). Default policy: refuse to cache to avoid bloat. Cloudflare normalizes Accept-Encoding internally, so Vary: Accept-Encoding doesn't multiply cache. Other CDNs (Akamai, Fastly) may need explicit "do cache despite Vary" config.
For Vary: Accept-Encoding with values gzip, br, identity — 3x storage per resource. Manageable. For Vary: Accept-Language with 100 locales — 100x, problematic. Vary: User-Agent fragments effectively infinitely; don't use it without explicit normalization.
`curl -H 'Accept-Encoding: gzip' -v URL` then `curl -H 'Accept-Encoding: identity' -v URL` — should return different responses (gzipped vs not). If both return same gzipped, CDN/cache isn't honoring Vary. If both return uncompressed, gzip itself isn't working.
Yes — `Vary: Accept-Encoding` covers all three. Caches differentiate based on which encoding the client sends in Accept-Encoding. Single Vary header value covers any number of compression formats your server supports.
Related articles
Gzip and Vary: Accept-Encoding Header
Vary: Accept-Encoding and SEO Benefits
Specify a Vary: Accept-Encoding Header