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

Specify a Vary: Accept-Encoding Header

3 min read
10.10.2025

The Vary: Accept-Encoding header is a response header used in HTTP to indicate that the content served to the client may vary based on the Accept-Encoding request header. This is particularly important for content compression, such as gzip or Brotli.

Specify Vary Accept-Encoding Header
Vary: Accept-Encoding — set per server; verify with curl.

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, Vary: Accept-Encoding and Content Negotiation, and Vary: Accept-Encoding and SEO Benefits.

Adding this header ensures that:

  1. Proper Caching: Caches (like CDNs and browsers) store different versions of the resource for different encoding types.
  2. Avoiding Conflicts: Prevents issues where cached compressed resources are incorrectly served to clients that do not support compression.

Why Use the Vary: Accept-Encoding Header?

  • Content Negotiation: Different clients (e.g., browsers) support different types of content encoding (gzip, Brotli, etc.). The Vary: Accept-Encoding header informs caches to store and serve the appropriate version.
  • Caching Compatibility: Ensures that caches like CDNs or proxies do not mistakenly serve compressed content to clients that do not support it.
  • SEO Benefits: Helps search engines index and cache the correct version of your resources.

In Apache

Modify the Apache configuration or .htaccess file.

  1. Open the .htaccess file in the root directory of your website.
  2. Add the following lines:
  3. <IfModule mod_headers.c>
    
        Header append Vary: Accept-Encoding
    
    </IfModule>
  4. Restart Apache to apply the changes:
  5. sudo systemctl restart apache2

In Nginx

Modify the Nginx configuration file.

  1. Open your Nginx configuration file (e.g., /etc/nginx/nginx.conf or a site-specific file in /etc/nginx/sites-available/).
  2. Add the following line inside the server or location block:
  3. gzip on;
    
    gzip_vary on;
  4. Save the file and reload Nginx:
  5. sudo systemctl reload nginx

In cPanel

If you use cPanel:

  1. Log in to cPanel.
  2. Go to File Manager and open the .htaccess file in the root directory.
  3. Add the following:
  4. <IfModule mod_headers.c>
    
        Header append Vary: Accept-Encoding
    
    </IfModule>
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

Using PHP

If you cannot modify the server configuration, add the header in your PHP scripts.

  1. Add the following line at the top of your PHP script:
  2. header("Vary: Accept-Encoding");

Verify the Vary Header

To confirm the header is applied:

  1. Use browser developer tools:
    • Open Developer Tools in your browser (F12).
    • Go to the Network tab.
    • Check the response headers of your resource.
  2. Use command-line tools:
  3. curl -I https://yourdomain.com

    Look for the Vary: Accept-Encoding header in the response.

  4. Use online tools like:

Best Practices

  1. Enable Compression: Combine the Vary: Accept-Encoding header with gzip or Brotli compression for better performance.
  2. Avoid Overusing Vary: Only use Vary headers where necessary to prevent cache fragmentation.
  3. Test for Issues: Ensure the header is applied correctly and that all compressed/uncompressed versions work as expected.
Frequently asked questions
mod_deflate adds Vary: Accept-Encoding to gzipped responses. Static files served as-is (no compression) won't have Vary. If your site has mixed content, use mod_headers to add Vary always: `Header always set Vary Accept-Encoding`. Keeps caches consistent.
Sends Vary header when nginx compresses. For pre-compressed files (`.gz` alongside `.html`), gzip_vary doesn't fire — use add_header. For mixed static + dynamic, add explicit `add_header Vary Accept-Encoding always;` in the relevant location blocks.
PHP-served dynamic responses where you want explicit control. `header('Vary: Accept-Encoding');` at script top. PHP scripts already going through web server compression (mod_deflate, nginx gzip) get Vary from the server. The PHP-side add is for direct application control regardless of server stack.
Origin server: Vary: Accept-Encoding on all responses. Compression at origin (gzip + brotli). CDN: normalize Accept-Encoding before cache key. This combination maximizes cache hits while serving correct compression to each client. Most production setups follow this.
Related articles
Gzip and Vary: Accept-Encoding Header
How to Add Vary: Accept Header Using cPanel
Vary: Accept-Encoding and Caching Compatibility