ProxyFix 的每个参数到底改了什么?子路径部署为什么链接全丢了前缀?只配 ProxyFix 够不够?这篇用同一个请求对比各参数,再用真实的 nginx 子路径验证(HTTPS 与 X-Forwarded-For 见第 134、135 篇)。
[没有 ProxyFix]
remote_addr=127.0.0.1 scheme=http host=internal:8000 script_root=''
url_for("items")=/items/ | 静态文件=/static/a.css | _external=http://internal:8000/items/
[x_for=1, x_proto=1]
remote_addr=10.0.0.1 scheme=https host=internal:8000 script_root=''
url_for("items")=/items/ | 静态文件=/static/a.css | _external=https://internal:8000/items/
[x_for=1, x_proto=1, x_host=1]
remote_addr=10.0.0.1 scheme=https host=shop.example.test script_root=''
url_for("items")=/items/ | 静态文件=/static/a.css | _external=https://shop.example.test/items/
[x_for=2]
remote_addr=203.0.113.5 scheme=https host=internal:8000 script_root=''
url_for("items")=/items/ | 静态文件=/static/a.css | _external=https://internal:8000/items/
[全部 =1(x_for/proto/host/port/prefix)]
remote_addr=10.0.0.1 scheme=https host=shop.example.test:8443 script_root='/app'
url_for("items")=/app/items/ | 静态文件=/app/static/a.css | _external=https://shop.example.test:8443/app/items/上一篇:Nginx 反向代理;下一篇:静态文件生产部署。
[没有 ProxyFix]
浏览器访问 /app/items/ → url_for = /items/ | 静态 = /static/a.css | 表单 action = /items/ | 重定向 Location = /items/
[ProxyFix(x_prefix=1),但 nginx 没传 X-Forwarded-Prefix]
浏览器访问 /app/items/ → url_for = /items/ | 静态 = /static/a.css | 表单 action = /items/ | 重定向 Location = /items/
[ProxyFix(x_prefix=1) + nginx 传 X-Forwarded-Prefix: /app]
浏览器访问 /app/items/ → url_for = /app/items/ | 静态 = /app/static/a.css | 表单 action = /app/items/ | 重定向 Location = /app/items/nginx 是自己起的实例(非默认端口),gunicorn 用 --forwarded-allow-ips 设成不信任回环,让 ProxyFix 来负责。