后处理 Nginx 反向代理响应的最佳方法
我正在研究从 Apache 切换到 Nginx 作为后端 Grails 应用程序前面的反向代理。 我正在尝试一些 URL 重写,并且遇到了从后端发回响应的问题。 我可以处理位置标头重写,但我想知道处理链接等实际内容的最佳方法是什么。
nginx_substitutions_filter 是首选方法还是人们使用另一个模块来在响应正文中进行内容替换?
我曾考虑过创建一个 Grails 插件来根据附加请求标头处理渲染正确的内容,但现在我认为最好在应用程序外部处理,以实现最大的灵活性和松散耦合。
是否有关于针对反向代理场景进行 URL 重写/响应后处理的最佳实践的文章?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Lua 模块捕获响应并像 Lua 字符串一样操作它。 将输出大写的愚蠢示例:
请参阅 http://wiki.nginx.org/HttpLuaModule# ngx.location.capture
You can use the Lua module to capture the response and manipulate it like a Lua string. Silly example to upper case the output:
see http://wiki.nginx.org/HttpLuaModule#ngx.location.capture
如果您只想替换标头,则 HeadersMore 3rd 方模块非常适合。
除此之外,susbstiution 模块似乎是唯一的选择。
但我建议你让后端返回正确的页面。 修改每个响应都会消耗资源并且需要时间。
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.