Django + Docker + SSL:如何强制CSRF在Nginx + Gunicorn + Docker堆栈上使用HTTPS?

时间:2018-02-11 17:13:07

标签: django ssl https csrf-protection django-csrf

通过https访问我的主机后问题开始我遇到DRF序列化我的图片时显示带有http://前缀的网址。然后我修改了下一个文件: 的 nginx.conf

server {
    server_name core.antu.ge;
    charset UTF-8;
    ssl on;
    listen 443 ssl;

    ssl_certificate /etc/ssl/certs/server.crt;
    ssl_certificate_key /etc/ssl/private/server.key;

    access_log  /var/log/nginx/core.access.log;
    error_log /var/log/nginx/core.error.log;

    location /static/ {
    autoindex on;
        root /;
    }

    location / {
        proxy_pass http://web:8000;
        proxy_pass_header X-CSRFToken;
        proxy_set_header X-Forwarded-Protocol https;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

settings.py

if not DEBUG:
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True

结果,我在drf中有https前缀,但是当我执行任何POST请求时,我得到了这个错误: enter image description here

P.S我不会禁用csrf中间件或写csrf_exempt

UPD 使用DEBUG = True一切正常:/

0 个答案:

没有答案