From 6fe5195b070dcc151419f8ccde957d80a093fce1 Mon Sep 17 00:00:00 2001 From: Zhengen TANG Date: Tue, 15 Sep 2026 09:55:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20waitress=20=E7=94=9F=E4=BA=A7=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E5=8A=A0=20trusted-proxy=20=E5=8F=82=E6=95=B0,?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=8D=E4=BB=A3HTTPS=E5=A4=B4=E8=A2=AB?= =?UTF-8?q?=E6=B8=85=E9=99=A4=E5=AF=BC=E8=87=B4=E7=9A=84=E9=87=8D=E5=AE=9A?= =?UTF-8?q?=E5=90=91=E6=AD=BB=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/windows/serve-backend.ps1 | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/deploy/windows/serve-backend.ps1 b/deploy/windows/serve-backend.ps1 index fdf058c..8c84999 100644 --- a/deploy/windows/serve-backend.ps1 +++ b/deploy/windows/serve-backend.ps1 @@ -18,4 +18,19 @@ if (-not (Test-Path $python)) { throw "找不到虚拟环境解释器:$python,请先创建虚拟环境并安装 requirements/production-windows.txt" } -& $python -m waitress --listen=127.0.0.1:8080 --threads=8 wagtailcms.wsgi:application +# --trusted-proxy / --trusted-proxy-headers 是必须的:Waitress 默认会清除所有 +# X-Forwarded-* 请求头(clear_untrusted_proxy_headers 默认为 True),除非请求的 +# REMOTE_ADDR 命中 --trusted-proxy 指定的地址。Nginx 与 Waitress 部署在同一台机器上, +# 反代请求的 REMOTE_ADDR 是 127.0.0.1,所以这里信任 127.0.0.1。 +# 如果不加这两个参数,Django 的 SecurityMiddleware 永远看不到 X-Forwarded-Proto, +# SECURE_SSL_REDIRECT 会认为请求不是 HTTPS 从而不断触发 301, +# 与 Nginx 的 HTTPS 反代形成死循环(redirect count exceeded)。 +# 注意:--trusted-proxy-headers 的值必须以空格分隔(Waitress 内部用 str.split(), +# 不支持逗号分隔),且必须作为单个参数传递(用引号包住),否则会被 Waitress 自身的 +# 合法性校验判定为"未知的 trusted_proxy_headers 值"直接抛异常、进程启动失败。 +& $python -m waitress ` + --listen=127.0.0.1:8080 ` + --threads=8 ` + --trusted-proxy=127.0.0.1 ` + "--trusted-proxy-headers=x-forwarded-for x-forwarded-proto x-forwarded-host x-forwarded-port" ` + wagtailcms.wsgi:application