每当我尝试登录Django的管理员应用程序时,在填写用户名和密码并提交表单后,该页面将永久挂起.
当我在浏览器中刷新POST请求时,我得到:
CSRF verification failed. Request aborted.
Run Code Online (Sandbox Code Playgroud)

当我再次刷新页面时,一切正常,我看到管理员应用程序,我已登录,我可以在我的应用程序和表格上进行CRUD操作.
有什么想法吗?
我在Django应用程序前面有Nginx反向代理做https握手.所以,当你去(例子):
https://platform.staging.com/admin/
Run Code Online (Sandbox Code Playgroud)
Nginx反向代理那个
http://admin1.staging.platform.com/admin/
Run Code Online (Sandbox Code Playgroud)
这是我的nginx配置:
upstream admin-cluster {
ip_hash;
server admin1.staging.platform.com;
}
# force redirect of http to https
# application will be available only over https
server {
listen 80 default;
server_name platform.staging.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
# https server
# traffic is going to local web servers over normal http
# front nginx proxy server will hold ssl session
server {
listen 443 ssl spdy;
server_name platform.staging.com;
keepalive_timeout 70;
ssl on;
ssl_certificate /etc/ssl/platform.staging.com.crt;
ssl_certificate_key /etc/ssl/platform.staging.com.key;
location /admin {
proxy_pass http://admin-cluster;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
所以,我找到了一个解决方法.当我在我的Nginx反向代理上同时允许端口80但是将所有http请求重定向到这样的https:
server {
listen 80 default;
server_name platform.staging.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
Run Code Online (Sandbox Code Playgroud)
它解决了这个问题.知道为什么吗?
小智 5
检查您的设置是否有覆盖SESSION_COOKIE_SECURE和CSRF_COOKIE_SECURE值的值,因为问题可能与CSRF cookie有关.编辑:我实际上这两个值设置为True.
另外,请检查您的SECURE_PROXY_SSL_HEADER设置,但我不确定它与您的问题有关.我在你的nginx conf文件中缺少一个proxy_set_header X-Forwarded-Protocol指令,用于让Django知道你正在通过一个代理.
| 归档时间: |
|
| 查看次数: |
3981 次 |
| 最近记录: |