Peleg
Peleg

Reputation: 1122

Forward request headers from nginx proxy server

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?

Upvotes: 93

Views: 245586

Answers (4)

PrestonDocks
PrestonDocks

Reputation: 5428

I am using Nginx Proxy Manager and was wondering why my request headers were not getting to my API. Adding ignore_invalid_headers off;

Adding proxy_pass_request_headers on; didn't work for me.

I was passing a header like this and it was not getting through Nginx

"User-Id": "17"

I updated the Advanced configuration for my Host with ignore_invalid_headers off; and everything worked as expected.

enter image description here

I have no idea why User-Id is an invalid header in the eyes of Nginx.

Upvotes: 0

sekrett
sekrett

Reputation: 1275

You may pass ALL headers by adding this:

ignore_invalid_headers off;

But please consider security issues by doing this.

Upvotes: 2

Peter
Peter

Reputation: 1893

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.

Upvotes: 81

Fleshgrinder
Fleshgrinder

Reputation: 16303

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.

Upvotes: 147

Related Questions