8

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.

Community
  • 1
  • 1
Gavin Ballard
  • 1,251
  • 2
  • 9
  • 14

2 Answers2

5

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;
}
foibs
  • 3,258
  • 1
  • 19
  • 13
-3

Trying to modify the POST data doesn't sound sensible. I suggest setting the extra param in the query string when you proxy_pass it and then read it as a GET variable.

proxy_pass https://example.com/external-endpoint/?extra_param=1;
Danack
  • 24,939
  • 16
  • 90
  • 122
  • It might not be sensible, but it's what I'm trying to do :). I don't have control over the service I'm `proxy_pass`ing to, so this isn't an option. Sorry! – Gavin Ballard Nov 13 '13 at 23:34
  • 2
    I came across this in google and this is useless, trying to add an API secret/client by proxy i.e. editing the post body is a very valid and very sensible requirement. – Ash Jul 03 '17 at 21:07