解决问题
最终,要解决此问题,所有链接和重定向都必须与Caddy知道如何处理的有效URL匹配。一定要给的东西;HTTP应用程序必须更正其URL以添加所需的子文件夹,或者您需要配置Caddy以使该应用程序具有所需的Web根目录。
1.上游
某些应用程序(例如Sonarr)是在假设它们可能被反向代理到子文件夹的情况下进行编程的。应用程序设置中有一个有用的选项-通常称为“ URL Base”-会将您的子文件夹作为其所有链接/标题的前缀添加。如果您代理的应用具有这样的功能,那就啦!设置并忘记它。如果没有,则不能选择该解决方案。
2.在球童中
A.放弃网络根目录
您可以改用子域,例如sonarr.example.com,并代理整个Web根。此配置可避免上述问题,并且绝对适用于任何HTTP应用程序。
B.通过HTML过滤/标题重写(高级)
或者,您可以尝试使用Caddy作为过滤层,并拦截那些不正确的链接,因为它们来自HTTP应用程序的下游。
Caddy 2模块已经完成,并且正在发布中,可以在响应正文中执行替换:https : //github.com/caddyserver/response-replace
使用该插件涉及定义规则,Caddy使用该规则来更改从上游HTTP应用程序接收到的HTML文档,然后再将其发送给客户端。这样,客户端将收到HTML中正确的链接。
使用Caddy的reverse_proxy指令已经可以实现从上游重写标头,该指令仅将标头更改为后端或从后端更改标头(或者,要更改任何/所有标头,您可以使用header伪指令。这样,您还可以修复Location重定向。如果仔细执行,这两种策略可以在子文件夹内生成一个包含整齐的HTTP应用程序,而无需该应用程序明确支持此行为。
Fixing the problem
Ultimately, to fix this issue, all links and redirects must match up to valid URLs that Caddy knows how to handle. Something’s gotta give; either the HTTP application must correct its URLs to add the subfolder you want, or you need to configure Caddy to let the app have the web root it wants.
- Upstream
Some apps, like Sonarr, are programmed with the assumption that they might be reverse proxied into a subfolder. There is a helpful option in the application’s settings – commonly referred to as a “URL Base” – which will add your subfolder as a prefix to all of its links/headers. If the app you’re proxying has a feature like this, hurrah! Set it and forget it. If not, this solution isn’t an option. - In Caddy
A. By giving up the web root
You can use a subdomain instead, e.g. sonarr.example.com, and proxy the entire web root. This configuration precludes the issue described above and will work for absolutely any HTTP application.
B. By HTML filtering/header rewriting (advanced)
Alternately, you could try to use Caddy as a filtering layer and intercept those incorrect links as they come downstream from your HTTP application.
A Caddy 2 module has been completed and is in the process of being published that can perform replacements in response bodies: https://github.com/caddyserver/response-replace
Using that plugin involves defining rules that Caddy uses to alter the HTML document it receives from the upstream HTTP application before sending it to the client. In this way, the client would receive the correct links in HTML.
Rewriting headers from upstream can be achieved already using Caddy’s reverse_proxy directive which changes headers only to or from the backend (or, to change any/all headers, you can use the header directive. In this way, you can fix Location redirects as well. Combined, if carefully executed, these two strategies can produce a neatly contained HTTP application inside a subfolder without requiring the app to explicitly support this behaviour.