$upstream_http_NAME and $sent_http_NAME vars not available in certain scopes

Maxim Dounin mdounin at mdounin.ru
Wed Mar 30 15:37:33 UTC 2016


Hello!

On Wed, Mar 30, 2016 at 07:11:45AM -0400, neilstuartcraig wrote:

> Hi all
> 
> I am developing a proxy service which uses NGINX to reverse proxy, kind of
> CDN-like but very specific to our needs. During this. I have hit an issue
> which I *think* is a bug but wanted to ask in case anyone can point to a
> solution or some reading I can do. The problem I have is this:
> 
> $upstream_http_NAME and $sent_http_NAME variables seem to be
> unpopulated/blank at some points in my config when other embedded variables
> *are* populated. This is probably best illustrated via an example, so here's
> a simplified test case I created:

[...]

> 			# This does match - for obvious reasons
> 			if ($upstream_http_via ~* ".*") {
> 
> 			# Problem: $upstream_http_via appears not to be populated at this
> point...
> 				set $via_comp "$upstream_http_via 1.y BLAH";
> 				more_set_headers "IF_VIA_COMP: Value is $via_comp";

[...]

> So my questions are:
> Can anyone see something I have done wrong?
> Is this expected behaviour (if yes, why? Seems strange some vars behave
> differently)
> Does anyone have a workaround or solution?

You are trying to use $upstream_http_via in the "if" and "set" 
directives, this is wrong.  The $upstream_http_via variable is 
only available once a response is returned by an upstream server, 
but the "if" and "set" directives are evaluated before the request 
is sent to the upstream server, see here:

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

As far as I understand from your config, you want to add something 
to the Via response header.  Correct approach would be to only use 
the header after the response is returned, e.g., in the 
"add_header" directive.  Try something like this:

    map $upstream_http_via $is_via {
        ""                 "";
        default            ", ";
    }

    server {
        ...

        location / {
            proxy_pass http://upstream;
            proxy_hide_header Via;
            add_header Via "$upstream_http_via$is_via1.1 foo"
        }
    }

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list