16

I have the following example

           location / {
                    proxy_read_timeout 2000;
                    proxy_next_upstream error;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header Host $http_host;
                    proxy_pass_header X_CUSTOM_HEADER;
                    proxy_redirect off;
                    proxy_max_temp_file_size 0;
                    proxy_pass https://prod;
                    break;
            }

Now when i use the following curl line

curl --head -H "X_CUSTOM_HEADER: foo" http://domain.com/api/test

Now that does not work.. the apache/php on the backend doesn't see the header. If I bypass nginx it works

curl --head -H "X_CUSTOM_HEADER: foo" http://web1.domain.com/api/test

4 Answers 4

18

You should use underscores_in_headers on directive which is off by default.

3
  • This info saves me hours. Dec 27, 2013 at 5:33
  • 1
    I would gladly buy you a refreshing beverage.
    – ballmw
    Jul 28, 2016 at 23:39
  • you sir, are a scholar and a gentleman. Jan 15, 2020 at 21:54
3

You should use proxy_set_header for all headers you wish to forward to the backend servers. So instead of proxy_pass_header ... line:

proxy_set_header X_CUSTOM_HEADER $http_x_custom_header;
2
  • I have tried that and it doesn't set the header correctly, If i replace $http_x_custom_header with "foo" it works
    – Mike
    Aug 3, 2011 at 21:23
  • I'm not sure why it didn't work for @Mike but it did work for me. I am using X-Forwarded-Proto with $http_x_forwarded_proto. Jul 9, 2014 at 21:43
2

The above didn't work for me either so I used proxy_pass_header. See the Nginx Wiki about proxy_pass_header here.

If your custom header is device_id add proxy_pass_header device_id; to your Proxy block.

If you are using customheaders with an underscore in it (like I am) be sure to make sure you have underscores_in_headers on in your Nginx Config.

1
  • Thanks a lot ! I was pulling my hair out.... Your post relieved me.... Apr 24, 2018 at 11:15
2

By default the nginx forwards all the ( proxy_pass_request_headers on;) the header to the backend server. But if your request header ( may be custom header) includes underscore ( _ ) in the header name then nginx blocks those headers.

Ex: authenticate_type, cdn_enable.

To enable Nginx to pass all or the custom requested header to the backend turn on the underscore option on.

underscores_in_headers on;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .