# Header handling

**URL:** <https://www.ruby-forum.com/t/header-handling/243011>\
**Category:** NGINX\
**Created:** [August 14, 2015, 6:51pm UTC](https://www.ruby-forum.com/t/header-handling/243011 "2015-08-14T18:51:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [August 14, 2015, 6:51pm UTC](https://www.ruby-forum.com/t/header-handling/243011/1 "2015-08-14T18:51:33Z")

</div>

Hi,

I have a few questions regarding headers in nginx:

1. I use proxy\_set\_header to pass a header to upstream servers. Is it  
possible to honor the header if the incoming request already has it?

2. I want to pass the “Server” header from upstream response to clients,  
and if there is no such response header, I’d like to add a customer one.  
Is  
it possible via core nginx or any third party modules? Currently I am  
using  
“proxy\_pass\_header Server” without any check. I am not sure what happens  
if  
upstream response doesn’t have it.

3. I am trying to log an upstream response header to access log but it  
has  
a “dot” in it (say X.header). I don’t have any control to the upstream  
servers. On nginx side, I tried setting “ignore\_invalid\_headers off” in  
the  
server block, and in the logformat, I tried a few things for the the  
column: $upstream\_http\_x.header $upstream\_http\_x\_header  
$upstream\_http\_x-header, but nothing works. Any ideas how I can log it?

Thanks!  
Frank

---

<div class="post-metadata">

**Author:** ![fjlj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e79b87/32.png) [@fjlj](https://www.ruby-forum.com/u/fjlj)\
**Post date:** [August 14, 2015, 7:37pm UTC](https://www.ruby-forum.com/t/header-handling/243011/2 "2015-08-14T19:37:43Z")

</div>

Hi,

1. Use mapped variable

map $http\_ $ {  
“” “value if nit set”;  
default $http\_;  
}  
Then you can set header with the new variable.

1. I guess you can use map, too. Use $upstream\_http\__name_ instead.

2. Sorry, I have no idea on this.

---

<div class="post-metadata">

**Author:** ![fjlj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e79b87/32.png) [@fjlj](https://www.ruby-forum.com/u/fjlj)\
**Post date:** [August 14, 2015, 10:04pm UTC](https://www.ruby-forum.com/t/header-handling/243011/3 "2015-08-14T22:04:56Z")

</div>

Thanks ryd994 for the suggestion! 1 and 2 are working now.  
Anyone else has any ideas on 3?

Frank

---

<div class="post-metadata">

**Author:** ![fjlj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e79b87/32.png) [@fjlj](https://www.ruby-forum.com/u/fjlj)\
**Post date:** [August 15, 2015, 9:16am UTC](https://www.ruby-forum.com/t/header-handling/243011/4 "2015-08-15T09:16:10Z")

</div>

I made the below patch and can now use $upstream\_http\_x\_header for  
logformat to capture the header X.header in the access log. Does anybody  
see any issues with the patch?

— src/http/ngx\_http\_variables.c.orig 2015-08-15 02:19:31.635328112  
+0000

+++ src/http/ngx\_http\_variables.c 2015-08-15 02:19:42.051541422 +0000

@@ -897,6 +897,8 @@

```
         } else if (ch == '-') {

             ch = '_';

```

- 

```
       } else if (ch == '.') {

```

- 

```
           ch = '_';

       }

       if (var->data[n + prefix] != ch) {

```

Thanks!  
Frank

---

<div class="post-metadata">

**Author:** ![fjlj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e79b87/32.png) [@fjlj](https://www.ruby-forum.com/u/fjlj)\
**Post date:** [August 17, 2015, 1:32pm UTC](https://www.ruby-forum.com/t/header-handling/243011/5 "2015-08-17T13:32:32Z")

</div>

Hello!

On Sat, Aug 15, 2015 at 12:15:47AM -0700, Frank L. wrote:

> ```
> } else if (ch == '-') {
> 
> ch = '_';
> 
> ```
> 
> - 
> 
> ```
> } else if (ch == '.') {
> 
> ```
> 
> - 
> 
> ```
> ch = '_';
> 
> }
> 
> ```

Such approach will likely result in security problems, as  
“X.header” and “X-header” would be indistinguishable from nginx  
point of view.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![fjlj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e79b87/32.png) [@fjlj](https://www.ruby-forum.com/u/fjlj)\
**Post date:** [August 18, 2015, 1:39am UTC](https://www.ruby-forum.com/t/header-handling/243011/6 "2015-08-18T01:39:51Z")

</div>

Hi Maxim,

Thanks for you comment! Do you have any other approaches/suggestions?  
I use nginx as a proxy/load-balancer. The request will be processed by  
the  
upstream java servers. I assume my change won’t actually modify the  
actual  
header, so upstream will still get the original header and can  
distinguish  
. and - ?

Regards,  
Frank
