Handler invokation after upstream server being picked

Jan Algermissen jan.algermissen at nordsc.com
Tue Nov 12 18:55:28 UTC 2013


> 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.

> 
> > 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.

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)

Jan


> 
> > To get hold of it, I try to find the index of the proxy_host var 
> > in my location config merge handler:
> > 
> >     var.data = (unsigned char*)"proxy_host";
> >     var.len = 10;
> >     child->proxy_host_var_index = ngx_http_get_variable_index(cf, &var);
> >     
> >     if (child->proxy_host_var_index == NGX_ERROR) {  /* NOT FOUND, TRY TO INHERIT */
> >         child->proxy_host_var_index = parent->proxy_host_var_index;
> >         return NGX_CONF_OK;
> >     }
> >
> >   /* FOUND - This loca config has its own proxy_pass configuration */
> >
> > In my handler, I then do this:
> >
> >    ngx_http_variable_value_t  *value;
> >    ...
> >     value = ngx_http_get_indexed_variable(r, conf->proxy_host_var_index);
> >
> > Does that make sense? I does not work for me, unfortunately.
> >
> > (The log module does see the proxy_host variable and logs the expected value for it.
> 
> See above for explanation why it doesn't work for you.  Looking
> into src/http/modules/ngx_http_proxy_module.c might be helpful,
> too.



More information about the nginx mailing list