Django + Gunicorn + Nginx = CSRF cookie not set



我已经设置Django背后的Nginx与Gunicorn,但当我试图登录到管理面板,我得到:

Forbidden (403)
CSRF verification failed. Request aborted.
Reason given for failure:
CSRF cookie not set.

这很奇怪,因为如果我在本地运行它可以正常工作。然而,在nginx后面,当我使用"python management .py runserver 0.0.0.0:8000"one_answers"python management .py run_gunicorn"运行它时,它失败了。

settings.py:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # CORS SUPPORT
    'corsheaders.middleware.CorsMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

nginx.conf:

server {
        listen 8080;
        server_name  example.com;
        access_log  /var/log/nginx/example.com.access.log;
        error_log  /var/log/nginx/example.com.error.log info;
        keepalive_timeout 5;
        location /assets/grappelli/ {
                alias /var/www/example.com/virtualenv/lib/python2.6/site-packages/grappelli/static/grappelli/;
        }
        location /assets/ { # STATIC_URL
                alias /var/www/example.com/PopcornHour/assets/; # STATIC_ROOT
                expires 30d;
        }
location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
    # what to serve if upstream is not available or crashes
    error_page 500 502 503 504 /media/50x.html;
}
非常感谢您的帮助!

我发现了它失败的原因——即使我在这个域禁用了Varnish,它仍然与头和cookie混淆,现在禁用它:)

最新更新