【问题标题】:Nginx + gunicorn Django 1.9 CSRF verification failedNginx + gunicorn Django 1.9 CSRF 验证失败
【发布时间】:2016-11-18 09:13:45
【问题描述】:

背景:
我正在尝试使用 django 配置 cloudflare flexible SSL。
浏览器 Cloudflare Nginx Gunicorn

问题:
我收到 CSRF 验证失败。管理面板登录请求中止 - 目前这是我网站上唯一的 POST 请求。 (相信我,我在这里和要点上浏览了大量帖子,但似乎没有什么对我有用:()

配置:

Nginx -

listen 80;
server_name domain.com;

real_ip_header X-Forwarded-For;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 104.16.0.0/12;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 199.27.128.0/21;
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
real_ip_header CF-Connecting-IP;

location / {
    proxy_pass http://unix:/var/webapps/run/SBWebsite.sock;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

Django - settings.py

DEBUG = False
PREPEND_WWW = True
USE_X_FORWARDED_HOST = True
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')

Cloudflare DNS -

A domain.com  points to <ip> Automatic
CNAME www is an alias of domain.com Automatic

Page rule is set to always use HTTPS

更新 #1
Chrome Incognito 模式下一切正常!

更新 #2(解决方案)
好像是 cookie 问题,我从浏览器中清除了所有 cookie,现在它工作正常!!
部分也可能是 SECURE_PROXY_SSL_HEADER 问题,因为它在我的设置中是错误的。py

【问题讨论】:

  • 页面是否被cloudflare缓存?
  • 检查 cloudflare 是否没有将 POST 请求替换为 GET
  • @frlan 我在 cloudflare 中禁用了 html 缓存
  • @Jerzyk 我刚刚从 nginx 日志中确认,POST 没有被 GET 取代

标签: python django nginx django-csrf


【解决方案1】:

在没有 Cloudflare 的类似设置中,我遇到了同样的错误:Nginx -> Gunicorn -> Django。

我通过更改此 Nginx 设置来修复它:
proxy_set_header X-Forwarded-Proto https;
致此:
proxy_set_header X-Forwarded-Proto $scheme;

这样方案不是硬编码的,而是从请求中派生出来的。

【讨论】:

    【解决方案2】:

    您在命名 http-header 时似乎犯了一个错误。您必须确保 X-Forwarded-Proto 标头的名称在 nginx 配置和 Django 的 settings.py 中都是正确的.

    因此您应该通过替换以下行来修改您的 settings.py 文件:

    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
    

    用这个:

    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    

    或者您可以在 nginx 配置文件中添加以下行:

        proxy_set_header X-Forwarded-Protocol https;
    

    【讨论】:

    • 我的位置块现在看起来像这样,这是正确的吗?我尝试重新启动 nginx 和 gunicorn,但错误仍然存​​在。 location / { proxy_pass http://unix:/var/webapps/run/SBWebsite.sock; 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; }
    • 我刚刚编辑了答案。似乎您的标题名称有误。尝试将 settings.py 中的 HTTP_X_FORWARDED_PROTO 与原始 nginx 配置文件一起使用。
    • 我尝试了两种方法,但错误仍然存​​在。即使我禁用 SSL(设置 CSRF_COOKIE_SECURE - FALSE),错误仍然存​​在。
    • 我刚从 chrome 隐身模式下尝试过,没有出现 csrf 错误。但是当我从普通窗口尝试时,我仍然得到 csrf 错误。
    • @John,尝试清理浏览器的缓存和与您的网站关联的 cookie
    猜你喜欢
    • 2016-05-29
    • 2018-03-14
    • 2012-09-08
    • 2015-06-02
    • 1970-01-01
    • 2017-03-29
    相关资源
    最近更新 更多