2

Gunicorn を使用して Nginx の背後に Django をセットアップしましたが、管理パネルにログインしようとすると、次のようになります。

Forbidden (403)
CSRF verification failed. Request aborted.

Reason given for failure:
CSRF cookie not set.

ローカルで実行すると正常に動作するため、これは奇妙です。ただし、nginx の背後では、「python manage.py runserver 0.0.0.0:8000」および「python manage.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;

}

助けてくれて本当にありがとうございます!

4

1 に答える 1

1

失敗した理由がわかりました-このドメインでVarnishを無効にしていても、ヘッダーとCookieが台無しになっていて、今のところ無効にしています:)

于 2013-04-08T19:12:18.450 に答える