gpt4 book ai didi

docker - Traefik (Docker) 没有设置 X-Forwarded-* header ?

转载 作者:行者123 更新时间:2023-12-02 18:06:07 26 4
gpt4 key购买 nike

我试图在 Docker 中运行 Apache,在 https 的 Traefik 反向代理后面。一切正常,除了当我访问没有斜杠的文件夹 URL 时,Apache 将我重定向到非 https(即 https://www.example.com/folder -> http://www.example.com/folder/ )。这是 Apache mod_dir DirectorySlash 引起的,如 here 所述& here .解决方案是使用重写规则,它在 DirectorySlash 之前启动,如下所示:

# Redirect to HTTPS before Apache mod_dir DirectorySlash redirect to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteCond %{LA-U:REQUEST_FILENAME} -d
RewriteRule ^/(.*[^/])$ https://%{HTTP_HOST}/$1/ [R=301,L,QSA]

然而, 问题是 Traefik 似乎没有设置 X-Forwarded-* header .这是我收到的标题的屏幕截图:
enter image description here

以下是我在 Apache docker-compose 文件中使用的标签:
  labels:
- traefik.enable=true
- traefik.port=80
- traefik.frontend.rule=PathPrefix:/web #Apache is accessible under https://example.com/web/

我尝试了各种标签组合,但无论我做什么, x-forwarded-* header 似乎总是丢失。例如( refref ):
- "traefik.frontend.headers.SSLProxyHeaders=X-Forwarded-Proto:https"
- "traefik.frontend.headers.SSLRedirect=true"

我什至尝试让 Traefik 添加我自己的自定义 header ,但无法显示这些 header ( ref ):
- "traefik.https.middlewares.testHeader.Headers.CustomRequestHeaders.X-Script-Name=test"

...然而,只是为了让自己相信我没有疯,这实际上是在 Traefik 后面运行的,而且 Traefik 可以添加我可以看到的标题,这确实有效并导致 X-Frame-Options 标题出现在 Firefox 中:
- traefik.frontend.headers.frameDeny=true

总而言之,问题是:为什么 Traefik 不设置 x-forwarded-* header (然后我可以在我的 Apache RewriteRules 中使用它) - 我怎样才能做到这一点?

最佳答案

对于任何发现这个并想知道的人,我的问题是双重的:

1) X-Forwarded-* header 在浏览器中不可见。您可以使用 phpinfo() 或通过转储 $_SERVER 变量在服务器上查看它们:

http://take.ms/cbhtM

2) 重定向不工作的原因(修复 DirectorySlash 问题)是因为除了上面列出的 RewriteRules 之外,你的 htaccess 必须包括 重写选项 AllowNoSlash .来自 Apache documentation :

By default, mod_rewrite will ignore URLs that map to a directory on disk but lack a trailing slash, in the expectation that the mod_dir module will issue the client with a redirect to the canonical URL with a trailing slash. [...] the AllowNoSlash option can be enabled to ensure that rewrite rules are no longer ignored. This option makes it possible to apply rewrite rules within .htaccess files that match the directory without a trailing slash, if so desired.

关于docker - Traefik (Docker) 没有设置 X-Forwarded-* header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55679620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com