14

I have the following setup and configured to send all /api requests to a different server:

location /api {
    proxy_pass              https://myapp.herokuapp.com;
    rewrite                 ^/api/(.*)              /$1     break;
}

My app sends a header (USER_CUSTOMER), when communicating directly with myapp.herokuapp.com from the app it works, but when requesting through the proxy server, the value appears NULL on the API Server.

The following works in NGINX, but I need the App to be able to set the value of USER_CUSTOMER.

location /api {
    proxy_pass              https://app.herokuapp.com;
    proxy_set_header        USER_CUSTOMER           ABC;
    rewrite                 ^/api/(.*)              /$1     break;
}

I may have additional headers to send in the future, so I'm hoping there is a flag to pass all headers from the proxy to the API Server.

Ubuntu
nginx/1.1.19

3 Answers 3

34

The header attribute USER_CUSTOMER is invalid syntax. Underscores are not valid in header attributes.

There is a workaround but best solution is to rewrite the attribute to valid syntax.

Workaround is to set the following where you specify the server name in config:

underscores_in_headers on;
3
  • 1
    Not work in my case, the error log shows: nginx: [emerg] "underscores_in_headers" directive is not allowed here in nginx.conf:55
    – thinkhy
    Apr 17, 2016 at 13:21
  • 1
    thx this worked!
    – Magico
    Dec 12, 2016 at 13:00
  • Worked!! But when request is called from the android app it shows me undefined. Nov 30, 2017 at 7:04
9

I think you are looking for proxy_pass_request_headers option. Set it to on:

location /api {
    proxy_pass_request_headers      on;
    proxy_pass                      https://app.herokuapp.com;
    proxy_set_header                USER_CUSTOMER              ABC;
    rewrite                         ^/api/(.*)                 /$1     break;
}
3
0

I faced this issue today (jul 2022), and it was fixed adding a final / to the location route.

I mean, this wasn't working properly for me

location /api {
    proxy_pass              https://myapp.herokuapp.com;
    rewrite                 ^/api/(.*)              /$1     break;
}

but when I defined it as:

location /api/ {
    proxy_pass              https://myapp.herokuapp.com;
    rewrite                 ^/api/(.*)              /$1     break;
}

it worked. Not sure about what the final slash is doing, but maybe this could help to someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.