我已经使用 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”运行它时它会失败。
设置.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;
}
非常感谢您的帮助!