Forward request headers from nginx proxy server

NginxProxyHttp Headers

Nginx Problem Overview


I'm using Nginx as a proxy to filter requests to my application. With the help of the "http_geoip_module" I'm creating a country code http-header, and I want to pass it as a request header using "headers-more-nginx-module". This is the location block in the Nginx configuration:

location / {
    proxy_pass                      http://mysite.com;
    proxy_set_header                Host http://mysite.com;;
    proxy_pass_request_headers      on;
    more_set_headers 'HTTP_Country-Code: $geoip_country_code';
}

But this only sets the header in the response. I tried using "more_set_input_headers" instead of "more_set_headers" but then the header isn't even passed to the response.

What am I missing here?

Nginx Solutions


Solution 1 - Nginx

If you want to pass the variable to your proxy backend, you have to set it with the proxy module.

location / {
    proxy_pass                      http://example.com;
    proxy_set_header                Host example.com;
    proxy_set_header                HTTP_Country-Code $geoip_country_code;
    proxy_pass_request_headers      on;
}

And now it's passed to the proxy backend.

Solution 2 - Nginx

The problem is that '_' underscores are not valid in header attribute. If removing the underscore is not an option you can add to the server block:

underscores_in_headers on;

This is basically a copy and paste from @kishorer747 comment on @Fleshgrinder answer, and solution is from: https://serverfault.com/questions/586970/nginx-is-not-forwarding-a-header-value-when-using-proxy-pass/586997#586997

I added it here as in my case the application behind nginx was working perfectly fine, but as soon ngix was between my flask app and the client, my flask app would not see the headers any longer. It was kind of time consuming to debug.

Solution 3 - Nginx

You may pass ALL headers by adding this:

ignore_invalid_headers off;

But please consider security issues by doing this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPelegView Question on Stackoverflow
Solution 1 - NginxFleshgrinderView Answer on Stackoverflow
Solution 2 - NginxPeterView Answer on Stackoverflow
Solution 3 - NginxsekrettView Answer on Stackoverflow