editing nginx http headers

we are trying to migrate from apache to nginx, and we have a setup where the proxy server receives an http header, modifies it and forwards it to the backend. This can easily be done with apache in the following way:

                Header edit* Custom_Header  "(String_To_Replace)" "Replacement"

However I cannot find any way of using regex in the nginx configuration. Any Ideas?

thanks


Solution 1:

You can edit headers using a map statement. See this document for details.

Example using the User-Agent request header.

map $http_user_agent $user_agent {
    default $http_user_agent;
    ~^(?<prefix>.*)Gecko(?<suffix>.*)$ "${prefix}Lizard$suffix";
}

The map block is placed outside of any server block. Edit the header using a proxy_set_header statement:

proxy_set_header User-Agent $user_agent;