【问题标题】:Referer checking failed ( Referer is insecure while host is secure)推荐人检查失败(推荐人不安全,而主机是安全的)
【发布时间】:2017-07-15 01:16:59
【问题描述】:

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

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

setting.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;
   }
 }

我该如何解决这个错误?

【问题讨论】:

  • 能不能在settings.py中添加SITE_ID = 1试试看。
  • 什么也没发生,仍然显示同样的错误。

标签: python django


【解决方案1】:

您似乎正在使用 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://

【讨论】:

  • 我的网站在http上,上面的Nginx conf文件显示得很好。访问 Django 管理页面时是否需要使用 https?当我尝试在生产环境中登录 Django 管理面板时,它会显示此错误,而不是在本地主机上!
  • 你为什么转发https头,proxy_set_header X-Forwarded-Proto https;
  • 不...我尝试将其注释掉,但仍然出现相同的错误。还有其他问题。
  • 你有没有注释掉你有 https 的所有 3 行,尤其是 UWSGI_SCHEME 也重新启动了 ngnix
猜你喜欢
  • 1970-01-01
  • 2011-02-17
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
相关资源
最近更新 更多