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

How to Specify a Vary: Accept-Encoding Header in Apache

3 min read
29.05.2025

Adding the Vary: Accept-Encoding header in Apache ensures that the server informs caching systems (browsers, CDNs, proxies) to store and serve different versions of a resource based on whether compression is used.

Specify Vary Accept-Encoding Header in Apache
Adding Vary: Accept-Encoding so proxies cache compressed and uncompressed copies separately.

For the modules and the broader Apache cache/headers setup, see How to Install and Enable mod_headers in Apache, How to Enable and Configure mod_headers in Apache, Understanding mod_headers in Apache, and How to Enable and Configure mod_expires in Apache for Caching.

Here is how to modify Apache to specify the Vary: Accept-Encoding header.

Enable Required Apache Modules

Ensure the following Apache modules are enabled:

  1. mod_headers: To modify HTTP headers.
  2. mod_deflate: To enable gzip compression (optional but recommended).

Run the following commands to enable these modules:

sudo a2enmod headers
sudo a2enmod deflate
sudo systemctl restart apache2

Add Vary: Accept-Encoding Header

Option 1: Add to the Apache Configuration

  1. Open the main Apache configuration file:
  2. sudo nano /etc/apache2/apache2.conf

    or, if you are working with virtual hosts:

    sudo nano /etc/apache2/sites-available/your-site.conf
  3. Add the following directive to specify the Vary: Accept-Encoding header:
  4. <IfModule mod_headers.c>
    
        Header append Vary: Accept-Encoding
    
    </IfModule>
  5. If gzip compression is enabled, ensure it works with Vary:
  6. <IfModule mod_deflate.c>
    
        AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
    
        Header append Vary: Accept-Encoding
    
    </IfModule>
  7. Save the file and exit.

Option 2: Use .htaccess File

If you do not have access to the server configuration files, you can modify the .htaccess file in your website root directory.

  1. Open or create a .htaccess file:
  2. nano /var/www/html/.htaccess
  3. Add the following lines:
  4. <IfModule mod_headers.c>
    
        Header append Vary: Accept-Encoding
    
    </IfModule>
    
    
    
    <IfModule mod_deflate.c>
    
        AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
    
        Header append Vary: Accept-Encoding
    
    </IfModule>
  5. Save and close the file.
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

Restart Apache

After making changes, restart Apache to apply the new configuration:

sudo systemctl restart apache2

Verify the Vary: Accept-Encoding Header

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

    Check the output for:

    Vary: Accept-Encoding
  3. Using Browser Developer Tools:
    • Open your browser Developer Tools (F12).
    • Go to the Network tab.
    • Reload your website and inspect the response headers.
  4. Online Tools: Use tools like WebPageTest or GTmetrix to confirm the header is applied.

Optional: Enable Compression for Better Results

  1. Ensure gzip or Brotli compression is enabled for your website.
  2. Example configuration for gzip in Apache:
  3. <IfModule mod_deflate.c>
    
        AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
    
    </IfModule>

Best Practices

  • Combine Vary: Accept-Encoding with proper cache-control headers for optimal caching.
  • Test regularly to ensure the header is applied correctly.
  • Avoid excessive Vary headers (e.g., Vary: Accept-Encoding, Accept-Language, User-Agent), as they may fragment the cache.
Frequently asked questions
Because gzip compression is the most common case of "same URL, different response body depending on a request header." Without the Vary header, a cache that picks up the gzipped copy serves it to clients that didn't send Accept-Encoding: gzip and they get an unreadable blob. Vary: Accept-Encoding makes the cache key include that header.
Yes — mod_deflate emits Vary: Accept-Encoding for any response it compresses. The explicit Header set line in the article is for cases where you compress in a different layer (mod_brotli, a CDN, an application server) and want to make sure Vary is always present regardless of which layer is producing the response.
Help. Without Vary, CDNs may refuse to cache compressed responses (safer than risking corruption), or they collapse all encodings into one cache entry and serve corrupt content. With Vary published, CDNs cache one entry per encoding and hit rate goes up.
Different syntax, same idea. add_header Vary Accept-Encoding always; in the relevant server/location block. gzip_vary on; in the http or server block is the shortcut that does the same thing automatically for any gzip-compressed response.
Related articles
Gzip and Vary: Accept-Encoding Header
How to Add Vary: Accept Header Using cPanel
Specify a Vary: Accept-Encoding Header