POST request body manipulation

Hello everyone,

i have a problem in configuring Nginx.

I have a location that serves as a proxy for a well-specified url
“/login”.
This location can receive both GET and POST request.
GET request have no body and should be proxied to a default and
well-know host.
POST request contains the host to be proxied to in their body
(extractable by a regexp).

To avoid use of “if”, i was using a map:

map $request_body $target_tenant_loginbody {
~account=(.)%40(?P<body_tenant>.)&password. $body_tenant;
default default.example.com;
}

    location /login {
             echo_read_request_body;

             proxy_pass http://$target_tenant_loginbody:9000;

             # Debug
             proxy_set_header X-Debug-Routing-Value

$target_tenant_loginbody;

             proxy_set_header          Host            $host;
             proxy_set_header          X-Real-IP $remote_addr;
             proxy_set_header          X-Forwarded-For

$proxy_add_x_forwarded_for;
}

This is not working (works with the GETs but not with the POSTs), seems
that the map returns always the default value even if the regexp works
(tested on regex101.com).
After a few tests, i understood that $request_body is empty or
non-initialized. I tried also with $echo_request_body, that seems
correctly initialized in location context but not in the map.

I read about a lot of issues and people having problem with empty
$request_body.

Maybe is there another approach you could direct me to?

Thanks in advance,
Sandro.


Questa e-mail stata controllata per individuare virus con Avast
antivirus.

I have the same requirement to map $request_body.

$request_body is logging to my access_log correctly via proxy_pass
setting.

I need to map $request_body to determine which part of it should be
logged,
as sometime it is too large to be logged, like uploading files.

any suggestions?

Posted at Nginx Forum:

On Thursday 17 December 2015 14:51:49 Valentin V. Bartenev wrote:

(extractable by a regexp).

    }

Module ngx_http_core_module
Module ngx_http_core_module

[…]

But in your case it’s empty because the $target_tenant_loginbody
variable
is evaluated before the body has been read.

So you can’t use it in the proxy_pass directive.

wbr, Valentin V. Bartenev

On Thursday 23 April 2015 18:52:00 Sandro Bordacchini wrote:

             proxy_pass http://$target_tenant_loginbody:9000;

Maybe is there another approach you could direct me to?

[…]

The $request_body variable is empty when the body doesn’t fit into the
“client_body_buffer_size”, or if the “client_body_in_file_only” is
enabled.

Also I suggest to turn on the “client_body_in_single_buffer” directive.

http://nginx.org/r/client_body_buffer_size
http://nginx.org/r/client_body_in_file_only
http://nginx.org/r/client_body_in_single_buffer

wbr, Valentin V. Bartenev

in my case, I can log the $request_body in the access_log via
proxy_pass,
even when uploading files more then 1MB.

I just wanted to limit the size of the $request_body in the log. Here is
my
nginx.conf:

http {
include mime.types;
default_type application/octet-stream;

map $request_body $request_body_short {
    "~^(?<SHORT>.*filename.*)Content-Type" $SHORT;
    default $request_body;
}

log_format  main  '$remote_addr - $remote_user [$time_local] 

“$request”

'$status $body_bytes_sent “$http_referer” “$http_user_agent”
“$http_x_forwarded_for” $request_time ’
‘“$request_body_short” $http_uid “$http_build”
“$http_appversion”
“$uid_got” “$host”’;

access_log  logs/access.log  main;

....

}

if I changed the map default value to something like ‘wrong’, the logged
$request_body would be ‘wrong’.

Posted at Nginx Forum: