15

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
dallasclark
  • 771
  • 2
  • 7
  • 17

3 Answers3

35

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;
dallasclark
  • 771
  • 2
  • 7
  • 17
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;
}
ek9
  • 2,093
  • 4
  • 19
  • 23
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.

Latra
  • 101