Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read and manipulate POST request variables with nginx?

Tags:

nginx

I'm using nginx (v1.4.1) to proxy an external service, but would like to be able to inspect and possibly modify the body of a POST request before proxying it. The problem is, I'm currently unable to access the POST request body while handling the request, whether through the $request_body variable or otherwise.

I've read a number of posts and SO questions, and have implemented this suggested strategy, which is intended to allow the logging of the POST body (when using a proxy_pass directive). However, while this works for me I'm still unable to read anything from $request_body during the processing of the request.

For clarity, here's the relevant section of my configuration:

location /proxy-this/ {
    client_max_body_size 8k;
    client_body_buffer_size 16k;
    client_body_in_single_buffer on;
    proxy_pass https://example.com/external-endpoint/;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_set_header Content-Length '';
}

And what I'd like to be able to do:

location /proxy-this/ {
    ... (same configuration as above) ...
    set $request_body $request_body&extra_param=1;
}

I'm aware that this is possible using the nginx_lua module, and have looked at the HttpFormInputModule, but the first seems overkill and the latter hasn't been updated in a while.

like image 612
Gavin Ballard Avatar asked Nov 13 '13 02:11

Gavin Ballard


1 Answers

Have you tried using proxy_set_body?

That would be like

location /proxy-this/ {
  ... (same configuration as above) ...
  proxy_set_body $request_body&extra_param=1;
}
like image 79
foibs Avatar answered Sep 18 '22 16:09

foibs