gpt4 book ai didi

python - Referer 检查失败(Referer 不安全,而主机安全)

转载 作者:太空宇宙 更新时间:2023-11-03 15:37:15 27 4
gpt4 key购买 nike

我陷入了这个错误。我将代码部署在生产服务器上,它在端口 80 上运行。当我尝试登录管理页面时。如图所示,它给了我 403 错误。

CSRF verification failed,Request aborted.

可能是什么原因?我的 Django 代码或 nginx 配置有问题吗?

设置.py文件

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'xxx'
DEBUG = True
ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]


ROOT_URLCONF = 'project.urls'


WSGI_APPLICATION = 'project.wsgi.application'

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

nginx 配置是:

server {
listen 80;
server_name www.example.com;


access_log /var/log/nginx/access.log;

error_log /var/log/nginx/error.log;
location /static/ {
alias /example/path/to/static/;
}

location / {
include /etc/nginx/sites-available/uwsgi_params;
uwsgi_pass unix:/path/to/project/yo.sock;
uwsgi_param UWSGI_SCHEME https;
uwsgi_pass_header X_FORWARDED_PROTO;
proxy_set_header X-Forwarded-Proto https;
}
}

如何解决此错误?

最佳答案

似乎您正在使用 HTTP 表单 URL 登录到 HTTPS 站点

在这种情况下,您会收到以下错误

 if request.is_secure():
# Ensure that our Referer is also secure.
if referer.scheme != 'https':
return self._reject(request, REASON_INSECURE_REFERER)

您应该将登录表单网址更改为 https://

关于python - Referer 检查失败(Referer 不安全,而主机安全),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42434708/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com