Header handling

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

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.

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

Frank

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

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.
http://nginx.org/

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