# Log response header from upstream

**URL:** <https://www.ruby-forum.com/t/log-response-header-from-upstream/213596>\
**Category:** NGINX\
**Created:** [November 6, 2011, 9:49pm UTC](https://www.ruby-forum.com/t/log-response-header-from-upstream/213596 "2011-11-06T21:49:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [November 6, 2011, 9:49pm UTC](https://www.ruby-forum.com/t/log-response-header-from-upstream/213596/1 "2011-11-06T21:49:07Z")

</div>

Hi,

I have some problem with access log combined with X-Accel-Redirect  
requests. In my case I have Nginx set up as load-balancer to group of  
application servers. These servers return some special header in every  
request - I need to log value of this special header (lets call it  
‘X-user’) in access log - also I do not want to expose this header to  
the world (proxy\_hide\_header helps here).

Everything seems to work, but when there is X-Accel-Redirect request I  
have empty field in access log because of subrequest  
($upstream\_http\_x\_special is cleared because of subrequest, if I  
understand this mechanism correctly). How to make this work for every  
request?

Here is my config:

http {  
log\_format extended ‘$request $upstream\_http\_x\_user’;  
access\_log /var/log/nginx/access.log extended;

…

server {  
listen 80;

```
location / {
  proxy_pass http://backend;
  proxy_hide_header X-User;
}

location /files {
  internal;
  proxy_pass http://filestore;
}

```

}  
}

Cheers,

–  
Kamil G.

---

<div class="post-metadata">

**Author:** ![grymuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@grymuz](https://www.ruby-forum.com/u/grymuz)\
**Post date:** [November 6, 2011, 10:15pm UTC](https://www.ruby-forum.com/t/log-response-header-from-upstream/213596/2 "2011-11-06T22:15:43Z")

</div>

Hello!

On Sun, Nov 06, 2011 at 09:48:08PM +0100, Kamil G. wrote:

> have empty field in access log because of subrequest  
> …  
> internal;  
> proxy\_pass [http://filestore](http://filestore);

Workaround is to use

```
    set $x_user $upstream_http_x_user;

```

here (and to log $x\_user instead).

> ```
> }
> 
> ```
> 
> }  
> }

Maxim D.

---

<div class="post-metadata">

**Author:** ![grymuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@grymuz](https://www.ruby-forum.com/u/grymuz)\
**Post date:** [November 7, 2011, 6:42am UTC](https://www.ruby-forum.com/t/log-response-header-from-upstream/213596/3 "2011-11-07T06:42:48Z")

</div>

Yeah, I have tried this, but this way non x-accel-redirect requests must  
be  
handled in old way which means I have to use 2 fields in access log. Is  
there any way to use only one field?  
06-11-2011 22:15 użytkownik “Maxim D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) napisał:

---

<div class="post-metadata">

**Author:** ![grymuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@grymuz](https://www.ruby-forum.com/u/grymuz)\
**Post date:** [November 7, 2011, 11:19am UTC](https://www.ruby-forum.com/t/log-response-header-from-upstream/213596/4 "2011-11-07T11:19:50Z")

</div>

Hello!

On Mon, Nov 07, 2011 at 06:42:09AM +0100, Kamil G. wrote:

> Yeah, I have tried this, but this way non x-accel-redirect requests must be  
> handled in old way which means I have to use 2 fields in access log. Is  
> there any way to use only one field?

E.g. you may use different log format for internal location.

Maxim D.

> > > application servers. These servers return some special header in every  
> > > Here is my config:  
> > > location / {  
> > > set $x\_user $upstream\_http\_x\_user;
> > 
> > * * *
> > 
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

---

<div class="post-metadata">

**Author:** ![grymuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@grymuz](https://www.ruby-forum.com/u/grymuz)\
**Post date:** [November 7, 2011, 12:29pm UTC](https://www.ruby-forum.com/t/log-response-header-from-upstream/213596/5 "2011-11-07T12:29:59Z")

</div>

Hello!

On Mon, Nov 07, 2011 at 11:57:15AM +0100, Kamil G. wrote:

> OK, thanks - it works.
> 
> One more question: I have to use this variables in quotes because  
> sometimes it resolves to just empty string and there is problem with  
> parsing such logs.
> 
> Why sometimes empty variables ends in single hyphen (“-”, this is what  
> I allways want) in access log and sometimes its just empty string  
> (“”)?

Hyphen is used when variable isn’t found. Empty string - if it’s  
found, but empty.

Maxim D.

---

<div class="post-metadata">

**Author:** ![grymuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@grymuz](https://www.ruby-forum.com/u/grymuz)\
**Post date:** [November 8, 2011, 12:19pm UTC](https://www.ruby-forum.com/t/log-response-header-from-upstream/213596/6 "2011-11-08T12:19:04Z")

</div>

> Hyphen is used when variable isn’t found. Empty string - if it’s  
> found, but empty.

I see that variable defined by user is always empty (no hyphen) even  
if it is not defined in location section connected with request (but  
defined somewhere else in nginx config - other location section not  
some section which is inherited). Is it normal?

Cheers,  
Kamil G.

---

<div class="post-metadata">

**Author:** ![grymuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@grymuz](https://www.ruby-forum.com/u/grymuz)\
**Post date:** [November 7, 2011, 11:58am UTC](https://www.ruby-forum.com/t/log-response-header-from-upstream/213596/7 "2011-11-07T11:58:22Z")

</div>

OK, thanks - it works.

One more question: I have to use this variables in quotes because  
sometimes it resolves to just empty string and there is problem with  
parsing such logs.

Why sometimes empty variables ends in single hyphen ("-", this is what  
I allways want) in access log and sometimes its just empty string  
("")?

–  
Kamil G.

---

<div class="post-metadata">

**Author:** ![grymuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@grymuz](https://www.ruby-forum.com/u/grymuz)\
**Post date:** [November 8, 2011, 2:31pm UTC](https://www.ruby-forum.com/t/log-response-header-from-upstream/213596/8 "2011-11-08T14:31:58Z")

</div>

Hello!

On Tue, Nov 08, 2011 at 12:18:29PM +0100, Kamil G. wrote:

> > Hyphen is used when variable isn’t found. Empty string - if it’s  
> > found, but empty.
> 
> I see that variable defined by user is always empty (no hyphen) even  
> if it is not defined in location section connected with request (but  
> defined somewhere else in nginx config - other location section not  
> some section which is inherited). Is it normal?

Yes.

Maxim D.
