sent_http_HEADER Volatile under Nginx 1.2.4

Maxim Dounin mdounin at mdounin.ru
Thu Dec 12 19:32:37 UTC 2013


Hello!

On Thu, Dec 12, 2013 at 07:19:56PM +0000, Paul Taylor wrote:

> I’m in the process of making some amends to an environment, 
> where my upstream servers are sending a custom header 
> (X-No-Cache), which I need to detect and alter caching rules 
> within the configuration.
> 
> The custom header is visible within the output, and I can 
> re-output it as another header through configuration (i.e. 
> add_header  X-Sent-No-Cache $sent_http_x_no_cache; ).
> 
> However, as soon as I perform any type of testing of this custom 
> header, it disappears.
> 
> For example, if I was to perform a map on the custom header, try 
> to set an Nginx variable to the value of the header, or test 
> within an IF statement, any future call to this header is no 
> longer possible. Additionally any setting or testing of the 
> header fails.

Both "set" and "if" directives you mentioned are executed _before_ 
a request is sent to upstream, and at this point there is no 
X-No-Cache header in the response.  Due to this, using the 
$sent_http_x_no_cache variable in "set" or "if" will result in an 
empty value, and this value will be cached for later use.

It's not clear what you are trying to do so I can't advise any 
further, but certainly using the $sent_http_x_no_cache variable in 
"if" or "set" directives isn't going to work, and this is what 
causes behaviour you see.

Just a map{} should work fine though - as long as you don't try to 
call the map before the X-No-Cache header is actually available.  
E.g., something like this should work fine:

    map $sent_http_x_no_cache $foo {
        ""                    empty;
        default               foo;
    }

    add_header X-Foo $foo;

It might be also a goo idea to use $upstream_http_x_no_cache 
variable instead, see here:

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#variables

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



More information about the nginx mailing list