100 lines
3.8 KiB
Plaintext
100 lines
3.8 KiB
Plaintext
# wagtailcms Windows Server 生产环境 Nginx 反向代理配置模板
|
||
# (Windows 版 nginx 不支持 Unix Domain Socket 反向代理到 gunicorn,
|
||
# 后端改用监听 127.0.0.1:8080 的 waitress,因此这里全部走 TCP 端口。)
|
||
#
|
||
# 使用方法:
|
||
# 1. 下载 nginx for Windows(http://nginx.org/en/download.html),解压到 C:\nginx
|
||
# 2. 将本文件内容追加/替换到 C:\nginx\conf\nginx.conf 的 http{} 块内
|
||
# (或用 include 语法:在 http{} 块中加一行 include wagtailcms.conf;,
|
||
# 并把本文件放到 C:\nginx\conf\wagtailcms.conf)
|
||
# 3. 将下面的 example.com / cms.example.com 替换为真实域名,
|
||
# ssl_certificate / ssl_certificate_key 替换为实际证书路径
|
||
# 4. nginx.exe -t 测试配置,nginx.exe -s reload 重载
|
||
#
|
||
# 架构:浏览器 -> Nginx(TLS 终止) -> {Next.js:3000(页面渲染) | waitress:8080(/admin /api /static /media 等后端路径)}
|
||
#
|
||
# 每个域名各拆成两个 server 块:80 端口只做 301 强制跳转到 https,
|
||
# 443 端口才真正反代到后端。不要把 listen 80/443 合并进同一个 server 块,
|
||
# 否则容易漏配 X-Forwarded-Proto,与 Django 的 SECURE_SSL_REDIRECT 互相 301,造成死循环。
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 后端管理域名(Wagtail 管理后台 / API),对应 WAGTAILADMIN_BASE_URL
|
||
# ---------------------------------------------------------------------------
|
||
|
||
# 80 端口只做强制跳转,不直接反代到后端——
|
||
# 否则 Django 的 SECURE_SSL_REDIRECT 会自己再 301 一次,且容易和下面的 443 块配置不一致导致循环跳转。
|
||
server {
|
||
listen 80;
|
||
server_name cms.example.com;
|
||
return 301 https://$host$request_uri;
|
||
}
|
||
|
||
server {
|
||
listen 443 ssl;
|
||
server_name cms.example.com;
|
||
|
||
# 证书路径按实际下载/申请到的文件替换(win-acme 或 阿里云 SSL 证书服务下载 Nginx 格式证书)
|
||
ssl_certificate C:/nginx/ssl/cms.example.com.pem;
|
||
ssl_certificate_key C:/nginx/ssl/cms.example.com.key;
|
||
|
||
client_max_body_size 20m;
|
||
|
||
location /static/ {
|
||
alias C:/wagtailcms/staticfiles/;
|
||
access_log off;
|
||
expires 30d;
|
||
}
|
||
|
||
location /media/ {
|
||
alias C:/wagtailcms/media/;
|
||
access_log off;
|
||
expires 7d;
|
||
}
|
||
|
||
location / {
|
||
proxy_pass http://127.0.0.1:8080;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
# 关键:必须显式写死 https,不要用 $scheme——
|
||
# 这个 server 块本身只监听 443,$scheme 理论上恒为 https,
|
||
# 但显式写死可以避免因为把该 location 误复制到 80 块时引发同样的死循环问题。
|
||
proxy_set_header X-Forwarded-Proto https;
|
||
}
|
||
}
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 前端站点域名(Next.js 页面),公众访问入口
|
||
# ---------------------------------------------------------------------------
|
||
server {
|
||
listen 80;
|
||
server_name www.example.com example.com;
|
||
return 301 https://$host$request_uri;
|
||
}
|
||
|
||
server {
|
||
listen 443 ssl;
|
||
server_name www.example.com example.com;
|
||
|
||
ssl_certificate C:/nginx/ssl/example.com.pem;
|
||
ssl_certificate_key C:/nginx/ssl/example.com.key;
|
||
|
||
client_max_body_size 20m;
|
||
|
||
location /_next/static/ {
|
||
proxy_pass http://127.0.0.1:3000;
|
||
proxy_set_header Host $host;
|
||
expires 30d;
|
||
access_log off;
|
||
}
|
||
|
||
location / {
|
||
proxy_pass http://127.0.0.1:3000;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
proxy_set_header X-Forwarded-Proto https;
|
||
}
|
||
}
|
||
|