Forum: NGINX Re: Handler invokation after upstream server being picked

5897ccfea0a62691f880792a3b417bbe?d=identicon&s=25 Jan Algermissen (Guest)
on 2013-11-12 19:56
(Received via mailing list)
> > can help a little with the code.
> 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
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2013-11-12 22:37
(Received via mailing list)
Hello!

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

> > > can help a little with the code.
> > 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).

> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.