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

sta*_*hip 5 django https http digital-ocean ubuntu-18.04

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

\n
python3 manage.py runserver 0.0.0.0:8000\n
Run Code Online (Sandbox Code Playgroud)\n

服务器启动。但是,一旦我尝试从 (my-rent-ip):8000 访问它,我会得到以下信息:

\n
[25/Aug/2021 01:49:01] code 400, message Bad request syntax ('\\x16\\x03\\x01\\x02\\x00\\x01\\x00\\x01\xc3\xbc\\x03\\x03G\xc3\x9b\xc3\xae}\xc3\x98\xc2\xb2\\x921\\x0e\\x94\\x17\\x9c\xc3\x8fe\xc2\xb9\\x88\xc3\xb1\xc3\xbf\xc3\x83\\x16\\x01\xc3\xa9\xc3\x96R\xc3\x9d\\x00\\x95F\\x8aG\\t\xc3\x89 8\xc2\xaf,\xc3\xba\xc3\x82\\x93\xc2\xb4\xc3\xb9\\x06\xc3\x9d\\x14\xc2\xbez\\x13\xc3\x82e4[\\x9a,.\xc3\xa6\\x96+$<~\\x8eq<\xc2\xb4\\t\\x00"ZZ\\x13\\x01\\x13\\x02\\x13\\x03\xc3\x80+\xc3\x80/\xc3\x80,\xc3\x800\xc3\x8c\xc2\xa9\xc3\x8c\xc2\xa8\xc3\x80\\x13\xc3\x80\\x14\\x00\\x9c\\x00\\x9d\\x00/\\x005\\x00')\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

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

\n

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

\n
\nfrom pathlib import Path\n\nfrom dotenv import load_dotenv   #for python-dotenv method\nload_dotenv()\n\n# Build paths inside the project like this: BASE_DIR / 'subdir'.\nBASE_DIR = Path(__file__).resolve().parent.parent\n\n\n# Quick-start development settings - unsuitable for production\n# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/\n\n# SECURITY WARNING: keep the secret key used in production secret!\nimport os \n\nSECRET_KEY = os.environ.get('SECRET_KEY')\n\n# SECURITY WARNING: don't run with debug turned on in production!\nDEBUG = False\n# DEBUG = os.environ.get('DJANGO_DEBUG', '') != 'False'\n\n\nALLOWED_HOSTS = ['*']\n\n\n# Application definition\n\nINSTALLED_APPS = [\n    'django.contrib.admin',\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n    'django.contrib.messages',\n    'django.contrib.staticfiles',\n    'pages',\n]\n\nMIDDLEWARE = [\n    'django.middleware.security.SecurityMiddleware',\n    'django.contrib.sessions.middleware.SessionMiddleware',\n    'django.middleware.common.CommonMiddleware',\n    'django.middleware.csrf.CsrfViewMiddleware',\n    'django.contrib.auth.middleware.AuthenticationMiddleware',\n    'django.contrib.messages.middleware.MessageMiddleware',\n    'django.middleware.clickjacking.XFrameOptionsMiddleware',\n]\n\nROOT_URLCONF = 'mysite.urls'\n\nTEMPLATES = [\n    {\n        'BACKEND': 'django.template.backends.django.DjangoTemplates',\n        'DIRS': [],\n        'APP_DIRS': True,\n        'OPTIONS': {\n            'context_processors': [\n                'django.template.context_processors.debug',\n                'django.template.context_processors.request',\n                'django.contrib.auth.context_processors.auth',\n                'django.contrib.messages.context_processors.messages',\n            ],\n        },\n    },\n]\n\nWSGI_APPLICATION = 'mysite.wsgi.application'\n\n\nSECURE_SSL_REDIRECT = True\nSESSION_COOKIE_SECURE = True\nCSRF_COOKIE_SECURE = True\n\n\n# Database\n# https://docs.djangoproject.com/en/3.2/ref/settings/#databases\n\nDATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.sqlite3',\n        'NAME': BASE_DIR / 'db.sqlite3',\n    }\n}\n\n\n# Password validation\n# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators\n\nAUTH_PASSWORD_VALIDATORS = [\n    {\n        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',\n    },\n    {\n        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',\n    },\n    {\n        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',\n    },\n    {\n        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',\n    },\n]\n\n\n# Internationalization\n# https://docs.djangoproject.com/en/3.2/topics/i18n/\n\nLANGUAGE_CODE = 'en-us'\n\nTIME_ZONE = 'UTC'\n\nUSE_I18N = True\n\nUSE_L10N = True\n\nUSE_TZ = True\n\n\n# Static files (CSS, JavaScript, Images)\n# https://docs.djangoproject.com/en/3.2/howto/static-files/\n\nSTATIC_URL = '/static/'\n\nSTATICFILES_DIRS = [\n    BASE_DIR / "static",\n    '/var/www/static/',\n]\n\n# Default primary key field type\n# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field\n\nDEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'\n
Run Code Online (Sandbox Code Playgroud)\n

太感谢了!

\n

Ala*_*ini 2

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

SECURE_SSL_REDIRECT=False
SESSION_COOKIE_SECURE=False
CSRF_COOKIE_SECURE=False
Run Code Online (Sandbox Code Playgroud)

也许现在您需要清除浏览器的缓存并重新启动服务器。该错误消息指的是开发服务器,因为python3 manage.py runserver仅在开发中使用。请注意,您应该在生产服务器中设置 https(当您设置 https 时,请再次设置我们之前更改为 True 的设置),并且设置 ALLOWED_HOSTS = ['*'] 是一个安全问题。

  • 实际上,我只是尝试将 http 而不是 https 放在 ip 前面,然后就加载了!太感谢了!现在,我必须弄清楚如何让 Django 支持 https。 (2认同)