Handler invokation after upstream server being picked

Maxim Dounin mdounin at mdounin.ru
Tue Nov 12 21:36:30 UTC 2013


Hello!

On Tue, Nov 12, 2013 at 07:55:28PM +0100, Jan Algermissen wrote:

> > Hello!
> > 
> > On Tue, Nov 12, 2013 at 03:28:48PM +0100, Jan Algermissen wrote:
> > 
> > > Maxim,
> > >
> > > a while ago you replied to my question below.
> > >
> > > Since yesterday I am trying to get hold of the proxy_host
> > > variable set by the proxy module but without success, maybe you
> > > can help a little with the code.
> > >
> > > From the source I understood that the proxy module sets the
> > > proxy_host var during NGINX startup. I guess, that happens on a
> > > per-location basis, depending on encountering a proxy_pass
> > > directive.
> > >
> > > Correct?
> > 
> > Not exactly.  Variables only exists during requests processing.
> > That is, the $proxy_host variable isn't set during nginx startup,
> > but instead its value becomes known when proxy module starts 
> > working with a request.
> 
> Ok. Sorry - I saw in the proxy code that 
> ngx_http_proxy_set_vars() is called inside the proxy_pass 
> directive handler. So I assumed they get set on startup.

It's called to cache appropriate values in location a 
configuration (plcf->vars) if there are no variables in 
proxy_pass.  These values are later used to initialize run-time 
data in ctx->vars once proxy starts handling a request (again, if 
there are no variables in proxy_pass).

> > > I should then be able to access the proxy_host variable during 
> > > the authentication phase (this is where my module sits).
> > > 
> > > Correct?
> > 
> > No, see above.  The $proxy_host variable value is not known till 
> > proxy started to work (and, if variables are used in proxy_pass, 
> > evaluated its parameter).
> 
> But the value of the proxy_host will never change after startup, 
> is that right? I mean, it will always be the value of the 
> proxy_host directve, or? At least when I log the var, it always 
> is.

In a simple configuration with proxy_pass without variables - it's 
always the same in a given location, yes.  It can be anything in a 
configuration like

    proxy_pass $backend;

though.

> Anyhow, do you have any suggestion how I best go about adding a 
> request header that is based on the proxy_host variable value.
> 
> (Background: I need to change the Authorization header, so that 
> request signature actually uses the right target host - see my 
> original question)

Write a configuration like this:

    proxy_pass http://backend.example.com;
    proxy_set_header Authorization $your_module_variable;

In your variable's get handler obtain $proxy_host variable, do 
needed calculations, and return a value you want to use in a 
request to an upstream server.

-- 
Maxim Dounin
http://nginx.org/en/donation.html



More information about the nginx mailing list