Home > Software engineering >  Apache2 - Rewrite Response header Key
Apache2 - Rewrite Response header Key

Time:10-12

How can I rewrite response headers within the apache2 conf? Is this even possible? We are using apache2 for legacy services which connects to a service behind an envoy proxy.

Envoy always set the response and request header to lower-case. Their solution with preserve case format doesn't work, because they don't respect the source header.

I expect a responseheader like

"DockingStation_One: 44"

envoy does

"dockingstation_one: 44"

with their preserve case format i get

"Dockingstation_one: 44"

My idea was to rewrite the header either with mod_rewrite or mod_headers in apache2 before it send the response to the client. Sadly nothing works as expected.

My latest setting:


<Location /my/service/v2>
        RewriteEngine   on
        # 1. Save the current value in env var (Case of header name does not matter)
        SetEnvIf dockingstation_one(.*) HEADER_VALUE=$1

        # 2. Delete the current header (Case of header name does not matter)
        Header unset "dockingstation_one" env=HEADER_VALUE

        # 3. Recreate header with the required case (Case of header name is preserved)
        #    env=HEADER_VALUE ensures the header is only set if it was set to begin with
        Header set "DockingStation_One" %{DOCKINGSTATION_ONE}e env=HEADER_VALUE


     ProxyPass        "http://service.go:8090/v2"
     ProxyPassReverse "http://service.go:8090/v2"
</Location>


CodePudding user response:

Nevermind. My whole syntax was wrong. My fix looks like this:

Header always set "DockingStation_One" "expr=%{resp:dockingstation_one}"
  • Related