首页 文章

Django Gunicorn Nginx =未设置CSRF cookie

提问于
浏览
2

我和Gunicorn一起在Nginx后面设置了Django,但是当我尝试登录管理面板时,我得到:

Forbidden (403)
CSRF verification failed. Request aborted.

Reason given for failure:
CSRF cookie not set.

这很奇怪,因为如果我在本地运行它可以正常工作 . 但是,当我使用“python manage.py runserver 0.0.0.0:8000”和“python manage.py run_gunicorn”运行它时,nginx后面会失败 .

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;

}

非常感谢您的帮助!

1 回答

  • 1

    我发现了为什么它失败了 - 即使我为这个域禁用了Varnish,它仍然与 Headers 和cookie混淆,暂时禁用它:)

相关问题