# Re: Handler invokation after upstream server being picked

**URL:** <https://www.ruby-forum.com/t/re-handler-invokation-after-upstream-server-being-picked/237426>\
**Category:** NGINX\
**Created:** [November 12, 2013, 7:56pm UTC](https://www.ruby-forum.com/t/re-handler-invokation-after-upstream-server-being-picked/237426 "2013-11-12T19:56:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [November 12, 2013, 7:56pm UTC](https://www.ruby-forum.com/t/re-handler-invokation-after-upstream-server-being-picked/237426/1 "2013-11-12T19:56:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jandot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jandot](https://www.ruby-forum.com/u/jandot)\
**Post date:** [November 12, 2013, 10:37pm UTC](https://www.ruby-forum.com/t/re-handler-invokation-after-upstream-server-being-picked/237426/2 "2013-11-12T22:37:22Z")

</div>

Hello!

On Tue, Nov 12, 2013 at 07:55:28PM +0100, Jan A. 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 D.  
[http://nginx.org/en/donation.html](http://nginx.org/en/donation.html)
