Files

37 lines
2.0 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 后端 WSGI 启动脚本(waitress,供 NSSM 注册为 Windows 服务时调用)。
#
# 用法(供 NSSM 服务的 Application 字段指向):
# powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\wagtailcms\deploy\windows\serve-backend.ps1
#
# 也可以直接双击/命令行手动运行,用于本地验证生产配置是否能正常启动。
$ErrorActionPreference = "Stop"
# 仓库根目录(本脚本位于 <repo>\deploy\windows\ 下)
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..")
Set-Location $RepoRoot
$env:DJANGO_SETTINGS_MODULE = "wagtailcms.settings.production"
$python = Join-Path $RepoRoot ".venv\Scripts\python.exe"
if (-not (Test-Path $python)) {
throw "找不到虚拟环境解释器:$python,请先创建虚拟环境并安装 requirements/production-windows.txt"
}
# --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