3

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

3 Answers 3

2

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;
1
  • Thank you for the answer Richard Smith, However, it doesn't seem possible to do the same with custom headers. In my case I want to modify the format of the client certificate and pass it to the backend. I cannot make it work with ssl_client_cert ` map $ssl_client_cert $user_agent { default $ssl_client_cert; ~^(?<prefix>.*)BEGIN(?<suffix>.*)$ "${prefix}TEST$suffix"; } ` Mar 9, 2020 at 13:12
0

I ended up using this configuration to remove the beginning ----- START CERTIFICATE -----, end certificate and the whitespaces in the received client cert.

map $ssl_client_raw_cert $a { "~^(-.*-\n)(?<main>[^\n]+)\n((?<b>[^\n]+)\n)?((?<c>[^\n]+)\n)?((?<d>[^\n]+)\n)?((?<e>[^\n]+)\n)?((?<f>[^\n]+)\n)?((?<g>[^\n]+)\n)?((?<h>[^\n]+)\n)?((?<i>[^\n]+)\n)?((?<j>[^\n]+)\n)?((?<k>[^\n]+)\n)?((?<l>[^\n]+)\n)?((?<m>[^\n]+)\n)?((?<n>[^\n]+)\n)?((?<o>[^\n]+)\n)?((?<p>[^\n]+)\n)?((?<q>[^\n]+)\n)?((?<r>[^\n]+)\n)?((?<s>[^\n]+)\n)?((?<t>[^\n]+)\n)?((?<v>[^\n]+)\n)?((?<u>[^\n]+)\n)?((?<w>[^\n]+)\n)?((?<x>[^\n]+)\n)?((?<y>[^\n]+)\n)?((?<z>[^\n]+)\n)?((?<ab>[^\n]+)\n)?((?<ac>[^\n]+)\n)?((?<ad>[^\n]+)\n)?((?<ae>[^\n]+)\n)?((?<af>[^\n]+)\n)?((?<ag>[^\n]+)\n)?((?<ah>[^\n]+)\n)?((?<ai>[^\n]+)\n)?((?<aj>[^\n]+)\n)?((?<ak>[^\n]+)\n)?((?<al>[^\n]+)\n)?((?<am>[^\n]+)\n)?((?<an>[^\n]+)\n)?((?<ao>[^\n]+)\n)?((?<ap>[^\n]+)\n)?((?<aq>[^\n]+)\n)?((?<ar>[^\n]+)\n)?((?<as>[^\n]+)\n)?((?<at>[^\n]+)\n)?((?<av>[^\n]+)\n)?((?<au>[^\n]+)\n)?((?<aw>[^\n]+)\n)?((?<ax>[^\n]+)\n)?((?<ay>[^\n]+)\n)?((?<az>[^\n]+)\n)?((?<ba>[^\n]+)\n)?((?<bb>[^\n]+)\n)?((?<bc>[^\n]+)\n)?((?<bd>[^\n]+)\n)?((?<be>[^\n]+)\n)?((?<bf>[^\n]+)\n)?((?<bg>[^\n]+)\n)?((?<bh>[^\n]+)\n)?((?<bi>[^\n]+)\n)?((?<bj>[^\n]+)\n)?((?<bk>[^\n]+)\n)?((?<bl>[^\n]+)\n)?((?<bm>[^\n]+)\n)?((?<bn>[^\n]+)\n)?((?<bo>[^\n]+)\n)?((?<bp>[^\n]+)\n)?((?<bq>[^\n]+)\n)?((?<br>[^\n]+)\n)?((?<bs>[^\n]+)\n)?((?<bt>[^\n]+)\n)*(-.*-)$"
$main }

and returned the vars in the location:

proxy_set_header WL-Proxy-Client-Cert $a$b$c$d$e$f$g$h$i$j$k$l$m$n$o$p$q$r$s$t$v$u$w$x$y$z$ab$ac$ad$ae$af$ag$ah$ai$aj$ak$al$am$an$ao$ap$aq$ar$as$at$au$av$aw$ax$ay$ax$ba$bb$bc$bd$be$bf$bg$bh$bi$bj$bk$bl$bm$bn$bo$bp$bq$br$bs$bt;
0

I manipulate headers like this (nginx):

location / {
    proxy_set_header Origin "https://developer.mozilla.org"; # edit header
    add_header Access-Control-Allow-Origin "*"; # add header
    ...
}

You must log in to answer this question.

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