您正在通过 HTTPS 访问开发服务器,但它仅支持 HTTP。代码 400,消息请求语法错误

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

我刚刚开始学习 Django 框架,我尝试在 DigitalOcean 的服务器上部署我的第一个项目。如果我跑步

python3 manage.py runserver 0.0.0.0:8000

服务器启动。然而,一旦我尝试从 (my-rent-ip):8000 访问它,我就会得到这个:

[25/Aug/2021 01:49:01] code 400, message Bad request syntax ('\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03GÛî}ز\x921\x0e\x94\x17\x9cÏe¹\x88ñÿÃ\x16\x01éÖRÝ\x00\x95F\x8aG\tÉ 8¯,úÂ\x93´ù\x06Ý\x14¾z\x13Âe4[\x9a,.æ\x96+$<~\x8eq<´\t\x00"ZZ\x13\x01\x13\x02\x13\x03À+À/À,À0̨̩À\x13À\x14\x00\x9c\x00\x9d\x00/\x005\x00')

[25/Aug/2021 01:49:01] You're accessing the development server over HTTPS, but it only supports HTTP

如果我运行生产服务器而不是开发服务器,这怎么可能?

我可能对

setting.py
做错了什么,因为出于生产目的,它必须进行很多更改。我创建了一个生产分支,更改了
settings.py
文件,并使用 GitHub 克隆到服务器。这是:


from pathlib import Path

from dotenv import load_dotenv   #for python-dotenv method
load_dotenv()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
import os 

SECRET_KEY = os.environ.get('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
# DEBUG = os.environ.get('DJANGO_DEBUG', '') != 'False'


ALLOWED_HOSTS = ['*']


# Application definition

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

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',
]

ROOT_URLCONF = 'mysite.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 = 'mysite.wsgi.application'


SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

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',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    BASE_DIR / "static",
    '/var/www/static/',
]

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

非常感谢!

django http https ubuntu-18.04 digital-ocean
3个回答
2
投票

您的服务器不支持 https,因此请进行以下设置:

SECURE_SSL_REDIRECT=False
SESSION_COOKIE_SECURE=False
CSRF_COOKIE_SECURE=False

现在您可能需要清除浏览器的缓存并重新启动服务器。 该错误消息指的是开发服务器,因为

python3 manage.py runserver
仅在开发中使用。 请注意,您应该在生产服务器中设置 https(当您设置 https 时,请再次设置我们之前更改为 True 的设置),并且设置 ALLOWED_HOSTS = ['*'] 是一个安全问题。


0
投票
Use http instead of https in url

我遇到了同样的问题,我可以在 ec2 ubuntu 20.04 上使用 http 访问它

在settings.py中使用

ALLOWED_HOSTS = ['*' ,'18.11.22.58']

确保更改 ec2

security group and allow traffic to port 8000

像这样运行服务器

python3 manage.py runserver 0.0.0.0:8000

尝试通过

http://18.11.22.58:8000/
(不是 https)访问您的网站

要了解如何在 https 上运行,请参阅此处:https://stackoverflow.com/a/51384868/3904109


0
投票

有同样的问题,我可以在 ec2 ubuntu 20.04 上使用 http 访问它

在settings.py中使用ALLOWED_HOSTS = ['*','18.11.22.58']

确保更改 ec2 安全组并允许流量到达端口 8000

像这样运行服务器 python3 manage.py runserver 0.0.0.0:8000

尝试通过以下方式访问您的网站: http://18.11.22.58:8000/(不是 https)

这对我有用

谢谢你

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