/ Work

Apache unexpectedly adding Vary: Host header to requests

So, I've been banging my head against an issue where Apache was unexpectedly adding the Vary: Host HTTP header to responses for certain requests. Unfortunately, this results in the responses not being cacheable by default by some proxies, most importantly in our case by Akamai.

After many Google searches resulted in nothing useful, I decided to just search through the Apache source code, and found this in modules/mappers/mod_rewrite.c:

/* If some HTTP header was involved in the condition, remember it
         * for later use
         */
        if (ctx->vary_this) {
            ctx->vary = ctx->vary
                        ? apr_pstrcat(r->pool, ctx->vary, ", ", ctx->vary_this,
                                      NULL)
                        : ctx->vary_this;
            ctx->vary_this = NULL;
        }
    }

So I went back through the Apache config, and did indeed find a RewriteCond rule that matched against the HTTP Host: header and altered the request accordingly.

The fix for this is simple, once the root cause was revealed -- add the NV (or novary) flag to the RewriteCond line, which (as documented here) results in:

If a HTTP header is used in the condition, this flag prevents this header from being added to the Vary header of the response.

Using this flag might break proper caching of the response if the representation of this response varies on the value of this header. So this flag should be only used if the meaning of the Vary header is well understood.

And now things are working as I expected. Hopefully this post will eventually percolate into Google and help someone else banging their heads against a wall about this in future!

Bryan Fullerton

Bryan Fullerton

Just a middle-aged dude doing his best to survive. Geeky Internet things from remote professionally.

Read More
Apache unexpectedly adding Vary: Host header to requests
Share this