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

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

4 min read
06.12.2025

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.

Nginx Vary Accept-Encoding
nginx — gzip_vary on; covers most cases automatically.

For closely related Vary / caching topics, see Gzip and Vary: Accept-Encoding, cPanel Vary: Accept Header, and Cache-Control: max-age — Complete Guide.

Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

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:

  1. Open your Nginx configuration file:
  2. sudo nano /etc/nginx/nginx.conf

    or for specific sites:

    sudo nano /etc/nginx/sites-available/your-site.conf
  3. Enable gzip compression:
  4. 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";
    • gzip on;: Enables gzip compression.
    • gzip_vary on;: Adds the Vary: Accept-Encoding header to responses.
    • gzip_types: Specifies the MIME types to compress.
  5. Save and close the file.

Add the Vary Header Manually

If you want to explicitly add the Vary: Accept-Encoding header (even without compression):

  1. Add the following directive inside the server or location block:
  2. add_header Vary "Accept-Encoding";
  3. Save and close the file.

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

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

    Look for:

    Vary: Accept-Encoding
  3. 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.
  4. Online Tools: Use tools like WebPageTest or GTmetrix to confirm the header is applied.

Best Practices

  1. Enable Compression: Use gzip or Brotli compression to reduce resource sizes and improve page load times.
  2. Avoid Overusing Vary: Avoid adding unnecessary Vary headers (e.g., Vary: Accept-Encoding, User-Agent, Accept-Language) to prevent excessive cache fragmentation.
  3. Combine with Proper Cache-Control: Use the Cache-Control header to define caching behavior alongside Vary.
  4. 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.

Frequently asked questions
gzip_vary only adds Vary when nginx actually compresses the response. For static files served as-is (no compression), Vary isn't added. If you serve pre-compressed assets (gzip-side-by-side files), add_header ensures Vary always present. For most use cases, gzip_vary is sufficient.
nginx's if has confusing semantics (often called "if is evil"). Add headers in location or server blocks, not if. Add `always` parameter to add_header so it applies even to non-200 responses: `add_header Vary Accept-Encoding always;` — without `always`, 304 Not Modified responses miss the header (browser then can't decide caching correctly).
`nginx -t` to test config syntax before reload. After `systemctl reload nginx`: `curl -I -H 'Accept-Encoding: gzip' https://yoursite.com/page.html` — response headers should include `Vary: Accept-Encoding`. If missing, the location block matching wasn't what you thought.
`Vary: Accept-Encoding` covers all compression negotiations. Whether nginx serves gzip or brotli depends on client's Accept-Encoding header and your module config. The Vary header is the same. Make sure both modules (ngx_http_gzip_module, ngx_brotli) are loaded and configured if you want to serve both.
Related articles
Gzip and Vary: Accept-Encoding Header
How to Add Vary: Accept Header Using cPanel
Specify a Vary: Accept-Encoding Header