How to rewrite the name of a backend header with nginx as a forward or reverse proxy?

25 Views Asked by At

I am looking for a way to rewrite the name of a header between two servers that use Nginx as frontend (reverse proxy) and as an output (forward proxy).

To be clear I want a request coming from the firtst server e.g.:

1. curl -X 'GET' 'https://my-serv-2/token' -H 'Authorization: Bearer XXX'
2. curl -X 'GET' 'https://my-serv-2/token' -H 'Custom-Authorization: Bearer XXX' 

To be read as the following on the second server:

1. curl -X 'GET' 'https://my-serv-2/token' -H 'Custom-Authorization: Bearer XXX'
2. curl -X 'GET' 'https://my-serv-2/token' -H 'Custom-Authorization: Bearer XXX' 

If possible I'd like to know how to do this either on the frontend part or nginx and on the exit part.

I have found a great deal of ressources on Nginx proxy_set_header but haven't managed to find a convincing solution as of now. Sorry if this is a duplicate.

For now I have this:

map $http_custom_authorization:$http_authorization $http_custom_authorization {
    default $http_custom_authorization;
    :* $http_authorization;
}

However wildcards don't work, I'd like Custom-Authorization to have the value of Authorization if it exists and keep its value otherwise.

0

There are 0 best solutions below