ChatGPT解决这个技术问题 Extra ChatGPT

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?

Also check if you are running nginx at a docker container inside a docker swarm cluster, if yes, then you will have to follow this steps

s
sandstrom

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.


I was just about to write you back that it doesn't work because I tried it already, but apparently it does. I wasn't viewing the request properly, but after using tcpdump I see that the header was passed this whole time... Thanks!
If it is set in the client. I just want that value passed down. I have no idea what the value is in my nginx set up so I cannot reset it. What I want is to have any custom headers created by the client pass through to the reverse-proxied server unchanged. Surely there is a way to do this.
also make sure than you use underscores_in_headers on; in your server block, else nginx is not forwarding those headers! ref: serverfault.com/a/586997/317502
Is that the correct way to pass the Host header? Host header is not supposed to include http://. Unless nginx is doing some magic to strip that out, I'd say the relevant proxy_set_header statement is wrong.
@Fleshgrinder would that work with 301/302 redirect as well?
P
Peter

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.


This is exactly the problem I was having with nginx and my search led me here as well. The accepted answer didn't make any difference.
in my case, the nginx was ignoring the header with an underscore(_), this answer solves my problem!
s
sekrett

You may pass ALL headers by adding this:

ignore_invalid_headers off;

But please consider security issues by doing this.