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.
Why Add the Vary: Accept-Encoding Header with Gzip?
- 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.
- 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.
- Improve Performance: For clients that support gzip, cached compressed resources are smaller and load faster, improving user experience.
- 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 headers
sudo systemctl restart apache2
Modify Configuration
Add the following to your .htaccess file or Apache configuration:
# 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
How to Enable Gzip and the Vary: Accept-Encoding Header in Nginx
Open Nginx Configuration
Open your Nginx configuration file:
Add Gzip Configuration
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
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:
Header append Vary: Accept-Encoding
</IfModule>
How to Verify Gzip and Vary: Accept-Encoding
- Using cURL: Check if gzip is enabled and Vary is set:
- Online Tools: Use tools like:
- Browser Developer Tools: Open your browser Developer Tools (F12). Go to the Network tab, reload the page, and inspect the response headers.
Look for:
Vary: Accept-Encoding
Best Practices
- 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.
- Monitor Performance: Regularly test your site performance and ensure compression is functioning.
- 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.


