通过HTTPS访问开发服务器,但在django Rest框架中仅支持HTTP错误

问题描述 投票:0回答:1

我实现了它,它似乎给了我一个错误(我目前的日志中没有确切的错误。它甚至可能与我当前遇到的错误相同)。我试图扭转它。没成功。

我去了ChatGPT。它给了我一些故障排除方法和解决方案。没有任何效果。

这是我的

settings.py

from pathlib import Path
import os

BASE_DIR = Path(__file__).resolve().parent.parent


SECRET_KEY = 'later'
DEBUG = False
ALLOWED_HOSTS = ['*']


INSTALLED_APPS = [
    'api',  # api is my app name
    'rest_framework',
    'rest_framework.authtoken',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.contenttypes',
]
MIDDLEWARE = [
    # 'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
]

ROOT_URLCONF = 'pythonRestApi.urls'
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',
            ],
        },
    },
]

WSGI_APPLICATION = 'pythonRestApi.wsgi.application'


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 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'},
]


LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True


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


DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


# This is the code which was reversed. It was all set to True
SECURE_BROWSER_XSS_FILTER = False
SECURE_CONTENT_TYPE_NOSNIFF = False

SECURE_SSL_REDIRECT = False

SECURE_HSTS_SECONDS = 0  # This one was set to 86400
SECURE_HSTS_PRELOAD = False
SECURE_HSTS_INCLUDE_SUBDOMAINS = False

SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False

这是我尝试进入 Rest API 站点时在终端中出现的错误:

code 400, message Bad request version ('ZZ\x13\x01\x13\x02\x13\x03À+À/À,À0̨̩À\x13À\x14\x00\x9c\x00\x9d\x00/\x005\x01\x00\x01\x93ÊÊ\x00\x00\x00')  // This seems to be changing for some reason every-time
You're accessing the development server over HTTPS, but it only supports HTTP.
code 400, message Bad HTTP/0.9 request type ('\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03\x18Ø&\x81é5Â')
You're accessing the development server over HTTPS, but it only supports HTTP.

这是我尝试使用

fetch.py
(它只是获取数据)获取它时的错误:

An error occurred: HTTPConnectionPool(host='192.168.29.72', port=80): Max retries exceeded with url: /auth/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000002D5090B52B0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

我试图确保我的网站安全。直到我偶然发现这个关于确保 Rest API 安全的视频。这个人指出了这篇文章,看起来不错。

django django-rest-framework
1个回答
0
投票

看来,我自己已经找到了解决方案。对于任何来这里寻求解决方案的人,只需将其设为

port=80
(如果您还没有)并仅禁用
SECURE_SSL_REDIRECT
。对我有用

© www.soinside.com 2019 - 2024. All rights reserved.