Variable “set” in map with an Header Value was lost when we delete the header.

Maxim Dounin mdounin at mdounin.ru
Wed Jan 12 18:36:58 UTC 2022


Hello!

On Wed, Jan 12, 2022 at 12:44:34PM -0500, podzol33 wrote:

> We have an responseHeader with technical information sent by the upstream
> server tomcat.
> We want to log this information in nginx and delete the header to avoid to
> be visible in the response Header to the client.
> 
> log_format formatjson escape=json '{ 
>     '"tomcat_container_id": "$TOMCAT_CONTAINER_ID" }';
> 
> Nginx.conf in http {
> map $sent_http_Container_Id $TOMCAT_CONTAINER_ID {
>     default $sent_http_Container_Id;
> }
> more_clear_headers 'Container-Id';
> 
> When I do this, my log tomcat_container_id is empty. 
> If I comment the more_clear_header command line, I have my log fill with the
> right value but the header is also sent to the client.
> So I don’t understand why my $TOMCAT_CONTAINER_ID Is clear when I delete the
> header and not clear if I don’t.

Maps are evaluated when you access resulting variable, so it is 
expected that the map result will be empty if there are no 
$sent_http_container_id during map evaluation, that is, at 
logging.  Overall, your construct is no different from using 
$sent_http_container_id in the log format directly.

As far as I understand what you are trying to do, proper solution 
would be to void both 3rd-party more_clear_headers and map, and 
instead use proxy_hide_header[1] to hide the header, and use the 
$upstream_http_container_id[2] variable for logging the original 
upstream response header.  Something like this:

   log_format formatjson escape=json '{ 
       '"tomcat_container_id": "$upstream_http_container_id" }';
   proxy_hide_header Container-Id;

Hope this helps.

[1] http://nginx.org/r/proxy_hide_header
[2] http://nginx.org/r/$upstream_http_

-- 
Maxim Dounin
http://mdounin.ru/



More information about the nginx mailing list