I need to relay a request to a Webservice that is not under my control and filter out bits and pieces and return the response to the initiator of the request.
By doing this, I can keep the location and credentials to this webservice secret to the initiator of the request and also filter out some of the data (the webservice doesn’t allow this directly). The filter could be a script of some sort (like a pipe: response from webservice -> filter -> return filtered response from webservice to serve request).
I have seen Nginx being used as reverse proxy (or perhaps even as proxy) with the ability to rewrite http headers or server names, etc, so I thought maybe this would be possible for Nginx. However, I haven’t actually seen (or found) an example of the paylod being altered. Perhaps I’m searching for the wrong keywords… Can anyone share some experience or point me in the right direction? I don’t necessarily use nginx, but I thought since I have some experience with it (and it offers authentication for instance and other thibgs), I’d start there.
CodePudding user response:
Here are some of your options:
standard module
ngx_http_sub_module
;more sophisticated module
ngx_http_substitutions_filter_module
(this one has regexp support; however, I know nothing about possible compatibility issues with the latest nginx versions);Lua scripting approach using
lua-nginx-module
/OpenResty. Here is an example rewriting both request and response body and headers: Is it possible to rewrite SOAP POST request bodies with Apache or Nginx? Nginx listen on any port but rewrite on 8080;JavaScript approach using njs module; if you need to deal with JSON responses, it can be the most convenient way. Check the
js_body_filter
directive.