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