Best method of post processing Nginx reverse proxy

2019-04-24 00:45发布

问题:

I'm doing some researching on switching from Apache to Nginx as a reverse proxy in front of a Grails application on the backend. I'm playing around with some URL rewriting and have run into an issue with the response being sent back from my back end. I can handle the location header rewrite but I'm wondering what the best way to process the actually content is for link and such.

Is nginx_substitutions_filter the preferred method or is there another module that folks use to do content replacement in the response body?

I've thought about creating a Grails plugin to handle rendering the correct content based on additional request headers but now I'm thinking that would be best handled outside the application to allow for the most flexibility and loose coupling.

Are there any articles about best practices for doing URL rewriting/response post processing for reverse proxy scenarios?

回答1:

You can use the Lua module to capture the response and manipulate it like a Lua string. Silly example to upper case the output:

res = ngx.location.capture('/some/path')
ngx.print(string.upper(res.body))

see http://wiki.nginx.org/HttpLuaModule#ngx.location.capture



回答2:

If you want to replace only the headers, HeadersMore 3rd party module is great for that. Other than that, susbstiution module seems to be the only option.

But I would suggest you make the backend return the correct page. Modifying every response uses resources and takes time.