【问题标题】:Why React DRF app deploying with Heroku gives Invalid HTTP_HOST header error?为什么使用 Heroku 部署的 React DRF 应用程序会出现 Invalid HTTP_HOST 标头错误?
【发布时间】:2020-01-23 00:03:13
【问题描述】:

我正在尝试使用 Heroku 部署我的第一个 React DRF 应用程序。它已成功部署(在 Heroku Dashboard 中查看发布日志下,显示没有要应用的迁移)。但是当我尝试访问该网站时,它给了我错误提示

Invalid HTTP_HOST header: 'mySite-test.herokuapp.com'. You may need to add 'mySite-test.herokuapp.com' to ALLOWED_HOSTS.

虽然我在 settings.py 中添加了相同的内容。我正在使用 gunicorn 版本 19.9.0 和 whitenoise 版本 4.1.3 。

到目前为止我做了什么

import os
import django_heroku

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DEBUG = True

ALLOWED_HOSTS = ['http://127.0.0.1:8000/', 'https://mySite-test.herokuapp.com/']


MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'personal_blog.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'build')],
        '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 = 'personal_blog.wsgi.application'

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'build/static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
django_heroku.settings(locals())

wsgi.py

import os
from whitenoise import WhiteNoise
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'personal_blog.settings')

application = get_wsgi_application()
application = WhiteNoise(application, root='build/static')

在项目根 urls.py 中

from django.views.generic import TemplateView

urlpatterns = [
    path('', include('accounts.api.urls')),
    re_path('.*', TemplateView.as_view(template_name='index.html'))

]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是我的文件夹结构

请有人指出我在这里做错了什么。

【问题讨论】:

    标签: reactjs heroku django-rest-framework


    【解决方案1】:

    主要问题在于 Python 版本。将 python 从 3.7.1 升级到 3.7.4 并在 settings.py 中修改为

    ALLOWED_HOSTS = ['127.0.0.1', '.herokuapp.com']

    现在一切正常。

    【讨论】:

      【解决方案2】:

      您不应该在允许的主机中包含 http 协议,它应该只是域名,可能还有端口,尽管我不完全确定。

      改变

       ALLOWED_HOSTS = ['http://127.0.0.1:8000/', 'https://mySite-test.herokuapp.com/']
      

      ALLOWED_HOSTS = ['127.0.0.1', 'mySite-test.herokuapp.com']
      

      【讨论】:

        【解决方案3】:

        Heroku 的域服务正在提供这种保护。 所以你不允许你的本地主机。

        
        ALLOWED_HOSTS = ['mySite-test.herokuapp.com']
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-04-08
          • 1970-01-01
          • 2020-03-01
          • 2016-04-10
          • 2017-02-04
          • 2013-02-15
          • 1970-01-01
          • 2018-11-23
          相关资源
          最近更新 更多