NGINX 反向代理 - 完全 "shadowing" 一个 URL 子树

NGINX Reverse Proxy - Completely "shadowing" an URL subtree

我有两台服务器,一个应用程序服务器 运行(后端)在 example.com:8000 监听,一个 NGINX 作为反向代理服务器(前端)在 example.com:443 监听。

最简单的部分是配置 NGINX,以便所有请求都代理到后端系统:

    location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass       http://127.0.0.1:8000;
    }

这很有魅力;但是还有另一个额外的要求,我还不知道如何实现。在后端系统中有一个 URL 子树被认为是 "ugly",比如 example.com:8000/my/ugly/path/,最终用户永远不会看到它,而是 "replaced" example.com:443/pretty-path/.问题是,后端系统不知道此子树的 "shadowing" 并生成包含 /my/ugly/path/ 的 URL(在 HTTP headers 和 HTML 内容中)。那么让/my/ugly/path/透明消失的最佳方法是什么?

您可以使用 sub_filter 指令。请注意,必须构建 nginx 以支持此功能;使用 nginx -V 显示配置选项,查找 --with-http_sub_module,或使用 nginx -t 测试您的配置文件。下面是未经测试的示例配置!

location / {
    sub_filter 'www.example.com/my/ugly/path'  
               'www.example.com/pretty/path';
    sub_filter_once off;
}

参考。 nginx sub_filter 文档 here.