Nginx + gunicorn Django 1.9 CSRF验证失败

时间:2021-07-17 07:22:40

Background :
I am trying to configure cloudflare flexible SSL with django.
Browser <-HTTPS-> Cloudflare <-HTTP-> Nginx <--> Gunicorn

背景:我正在尝试用django配置cloudflare flexible SSL。浏览器<- https -> Cloudflare <- http -> Nginx <- > Gunicorn

Issue :
I am getting CSRF verification failed. Request aborted for admin panel login - For now this is the only POST request on my website. (belive me, I have skimmed through tons of posts here and on gist but nothing seems to be working for me :( )

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

Configurations :

配置:

Nginx -

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

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 -

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

Update #1
Everything works fine from Chrome Incognito mode !

更新#1所有工作正常,从Chrome隐身模式!

Update #2 (Solution)
Seems like it was cookie issue, I cleared all cookies from my browser and now its working fine!!
Partly it could also be SECURE_PROXY_SSL_HEADER issue as it was wrong in my settings.py

更新#2(解决方案)似乎是cookie问题,我清除了浏览器中的所有cookie,现在它工作得很好!在某种程度上,它也可能是SECURE_PROXY_SSL_HEADER问题,因为它在我的settings.py中是错误的

2 个解决方案

#1


1  

It seems you have made a mistake while naming http-header. You have to ensure that the name of the X-Forwarded-Proto header is correct in both: nginx configuration and Django's settings.py.

似乎您在命名http-header时犯了一个错误。您必须确保x -转发- proto头部的名称在nginx配置和Django的设置中都是正确的。

So you should modify your settings.py file by replacing the line:

所以你应该修改你的设置。py文件替换行:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')

with this one:

这一个:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

Or you can add to nginx configuration file the line below:

或者您可以添加到nginx配置文件如下:

    proxy_set_header X-Forwarded-Protocol https;

#2


0  

I was getting the same error in a comparable setup without Cloudflare: Nginx -> Gunicorn -> Django.

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

I fix it by changing this Nginx setting:
proxy_set_header X-Forwarded-Proto https;
To this:
proxy_set_header X-Forwarded-Proto $scheme;

我通过更改Nginx设置来修复它:proxy_set_header x - forward - proto https;为此:proxy_set_header x - forward - proto $scheme;

So that the scheme is not hard-coded, but instead derived from the request.

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

#1


1  

It seems you have made a mistake while naming http-header. You have to ensure that the name of the X-Forwarded-Proto header is correct in both: nginx configuration and Django's settings.py.

似乎您在命名http-header时犯了一个错误。您必须确保x -转发- proto头部的名称在nginx配置和Django的设置中都是正确的。

So you should modify your settings.py file by replacing the line:

所以你应该修改你的设置。py文件替换行:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')

with this one:

这一个:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

Or you can add to nginx configuration file the line below:

或者您可以添加到nginx配置文件如下:

    proxy_set_header X-Forwarded-Protocol https;

#2


0  

I was getting the same error in a comparable setup without Cloudflare: Nginx -> Gunicorn -> Django.

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

I fix it by changing this Nginx setting:
proxy_set_header X-Forwarded-Proto https;
To this:
proxy_set_header X-Forwarded-Proto $scheme;

我通过更改Nginx设置来修复它:proxy_set_header x - forward - proto https;为此:proxy_set_header x - forward - proto $scheme;

So that the scheme is not hard-coded, but instead derived from the request.

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