# Logging header values from the first upstream

**URL:** <https://www.ruby-forum.com/t/logging-header-values-from-the-first-upstream/236915>\
**Category:** NGINX\
**Created:** [October 11, 2013, 12:27am UTC](https://www.ruby-forum.com/t/logging-header-values-from-the-first-upstream/236915 "2013-10-11T00:27:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [October 11, 2013, 12:27am UTC](https://www.ruby-forum.com/t/logging-header-values-from-the-first-upstream/236915/1 "2013-10-11T00:27:34Z")

</div>

The HttpUpstreamModule states that variable values for  
$upstream\_http\_$HEADER are only valid for the last upstream accessed  
during a request. I’d like to know if there’s a workaround.

Specifically, I’m setting (and then clearing) headers in my first  
upstream to get request-specific information like ‘username’. Here’s  
what I’ve got:

===

http {  
log\_format myapp '$remote\_addr [$time\_local] ’  
'$request\_time ’  
'"$request" $status ’  
'$request\_length $body\_bytes\_sent ’  
‘"$upstream\_addr" $upstream\_response\_time’  
'$upstream\_http\_x\_myapp\_username ';  
…

```
server {
    location /api/ {
        ...
        access_log /var/log/nginx/myapp_access.log myapp;

        proxy_pass http://myapp_upstream;

        more_clear_headers 'X-MyApp-Username';
        ...
    }

    location ~* /internal/media_url/(.*) {
        # only allowed for internal redirects (X-Accel-Redirect,

```

included)  
internal;

```
        # still need to set our access log to get the original

```

request  
access\_log /var/log/nginx/myapp\_access.log myapp;

```
        proxy_pass $1;
     }
}

```

}

===

Some of my /api accesses involve accessing media that is hosted  
elsewhere, so I use  
the X-Accel-Redirect header to redirect to that location.  
Obviously, when the first location redirects to /internal/media\_url/\*,  
the reference to $upstream\_http\_x\_myapp\_username in my log line refers  
to the X-MyApp-Username header that is returned by the media server,  
which is empty.

How can I log the username with my requests that result in an internal  
redirect when the information is only available from the first upstream?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [October 11, 2013, 12:56am UTC](https://www.ruby-forum.com/t/logging-header-values-from-the-first-upstream/236915/2 "2013-10-11T00:56:33Z")

</div>

Hello!

On Fri, Oct 11, 2013 at 12:27:34AM +0200, Matt Spitz wrote:

> The HttpUpstreamModule states that variable values for  
> $upstream\_http\_$HEADER are only valid for the last upstream accessed  
> during a request. I’d like to know if there’s a workaround.

[…]

The $upstream\_\* variables are cleared once nginx starts to work  
with a new upstream. If you want to preserve values before  
X-Accel-Redirect, you may do so by using “set” in a destination  
location of X-Accel-Redirect (this works as rewrite directives are  
executed before upstream data is re-initialized). E.g.:

```
location /api/ {
    proxy_pass ...
}

location /internal/ {
    set $previous_upstream_http_foo $upstream_http_foo;
    proxy_pass ...
}

```

–  
Maxim D.  
[http://nginx.org/en/donation.html](http://nginx.org/en/donation.html)

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [October 11, 2013, 5:33am UTC](https://www.ruby-forum.com/t/logging-header-values-from-the-first-upstream/236915/3 "2013-10-11T05:33:37Z")

</div>

Perfect. Thank you very much!
