How to Specify a Vary: Accept-Encoding Header in Nginx
The Vary: Accept-Encoding HTTP response header in Nginx ensures that caching systems (like CDNs, proxies, or browsers) differentiate between compressed and uncompressed versions of a resource. This is essential for ensuring compatibility, improving performance, and avoiding cache mismatches.
For closely related Vary / caching topics, see Gzip and Vary: Accept-Encoding, cPanel Vary: Accept Header, and Cache-Control: max-age — Complete Guide.
Steps to Add Vary: Accept-Encoding in Nginx
Enable Compression
To use the Vary: Accept-Encoding header effectively, you need to enable compression (e.g., gzip or Brotli). Here is how:
- Open your Nginx configuration file:
- Enable gzip compression:
gzip on;: Enables gzip compression.gzip_vary on;: Adds theVary: Accept-Encodingheader to responses.gzip_types: Specifies the MIME types to compress.- Save and close the file.
sudo nano /etc/nginx/nginx.conf
or for specific sites:
sudo nano /etc/nginx/sites-available/your-site.conf
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1000;
gzip_disable "msie6";
Add the Vary Header Manually
If you want to explicitly add the Vary: Accept-Encoding header (even without compression):
- Add the following directive inside the
serverorlocationblock: - Save and close the file.
add_header Vary "Accept-Encoding";
Test Configuration
Before reloading Nginx, test the configuration for syntax errors:
sudo nginx -t
Reload Nginx
Apply the changes by reloading Nginx:
sudo systemctl reload nginx
Verify the Vary: Accept-Encoding Header
- Using cURL: Run the following command:
- Browser Developer Tools:
- Open your browser Developer Tools (F12).
- Go to the Network tab.
- Reload your website and inspect the response headers for the Vary header.
- Online Tools: Use tools like WebPageTest or GTmetrix to confirm the header is applied.
curl -I https://yourdomain.com
Look for:
Vary: Accept-Encoding
Best Practices
- Enable Compression: Use gzip or Brotli compression to reduce resource sizes and improve page load times.
- Avoid Overusing Vary: Avoid adding unnecessary Vary headers (e.g.,
Vary: Accept-Encoding, User-Agent, Accept-Language) to prevent excessive cache fragmentation. - Combine with Proper Cache-Control: Use the
Cache-Controlheader to define caching behavior alongside Vary. - Test Regularly: Verify the header is applied correctly and resources are served based on client capabilities.
Example Configuration
Here is a complete example Nginx configuration for enabling Vary: Accept-Encoding and gzip compression:
server {
listen 80;
server_name yourdomain.com;
location / {
root /var/www/yourdomain;
index index.html index.htm;
# Enable gzip compression
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1000;
# Add Vary: Accept-Encoding manually (optional)
add_header Vary "Accept-Encoding";
}
}
By following these steps, you can ensure that the Vary: Accept-Encoding header is properly applied in your Nginx server, improving caching compatibility and overall website performance.


