如何修复 Digital Ocean 上的 Django 错误:DisallowedHost at / Invalid HTTP_HOST header: ... 您可能需要将 ... 添加到 ALLOWED_HOSTS

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

我正在尝试使用 Django 框架开发一个网站,并使用 DigitalOcean.com 启动并将必要的文件部署到 django-project 中。

我必须将静态文件包含到 Django-project 中,收集静态文件后,我尝试刷新我的 ip

我包括了我用来创建网站的教程。 https://www.pythonprogramming.net/django-web-server-publish-tutorial/

我收到以下错误:

DisallowedHost at / 无效的 HTTP_HOST 标头:“198.211.99.20”。您可以 需要将 u'198.211.99.20' 添加到 ALLOWED_HOSTS。

有人可以帮我解决这个问题吗?这是我的第一个使用 Django 框架的网站。

python django python-2.7 http-host
6个回答
290
投票

错误日志很简单。正如它所建议的,您需要将 198.211.99.20 添加到您的

ALLOWED_HOSTS
设置中。

在您的项目 settings.py 文件中,像这样设置

ALLOWED_HOSTS
:

ALLOWED_HOSTS = ['198.211.99.20', 'localhost', '127.0.0.1']

进一步阅读 从这里阅读。


9
投票

设置.py

ALLOWED_HOSTS = ['*'] // if you are in dev or docker

已编辑

好吧,如果您不使用 docker,请不要在生产中执行此操作,只需输入 IP 地址即可。

问候


2
投票

在您的项目 settings.py 文件中,像这样设置 ALLOWED_HOSTS :

ALLOWED_HOSTS = ['62.63.141.41', 'namjoosadr.com']

然后重新启动你的apache。在 ubuntu 中:

/etc/init.d/apache2 restart

1
投票

您可以将

ALLOWED_HOSTS
添加到您的设置文件或
env
文件中:

ALLOWED_HOSTS = [".localhost", "127.0.0.1", "[::1]"]

0
投票

如果没有其他答案,你可以尝试修改manage.py并添加这三行

from django.utils.regex_helper import _lazy_re_compile
import django.http.request
django.http.request.host_validation_re = _lazy_re_compile(r"[a-zA-z0-9.:]*")

最终得到这样的结果:

import os
import sys

from django.utils.regex_helper import _lazy_re_compile
import django.http.request    
django.http.request.host_validation_re = _lazy_re_compile(r"[a-zA-z0-9.:]*")

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project01.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

正如这篇文章中所解释的:如何解决开发过程中 Django 中的“根据 RFC 1034/1035 提供的域名无效”


0
投票

我正在

minikube
上测试我的 Django 应用程序,由于探针的原因,它失败并出现以下错误:

Invalid HTTP_HOST header: '10.244.0.8:8080'. You may need to add '10.244.0.8' to ALLOWED_HOSTS.
Bad Request: /

我在我的探针中添加了

httpHeaders
,之后它就起作用了。

之前:

livenessProbe:
  httpGet:
    path: / # Replace with an endpoint that returns a 200 status code if the app is healthy
    port: 8080
  initialDelaySeconds: 10 # Delay before the first probe is executed
  periodSeconds: 10 # How often to perform the probe
readinessProbe:
  httpGet:
    path: / # Replace with an endpoint that indicates readiness
    port: 8080
  initialDelaySeconds: 5 # Delay before the first probe is executed
  periodSeconds: 5 # How often to perform the probe

之后:

livenessProbe:
  httpGet:
    path: / # Replace with an endpoint that returns a 200 status code if the app is healthy
    port: 8080
    httpHeaders:
    - name: Host
      value: localhost
  initialDelaySeconds: 10 # Delay before the first probe is executed
  periodSeconds: 10 # How often to perform the probe
readinessProbe:
  httpGet:
    path: / # Replace with an endpoint that indicates readiness
    port: 8080
    httpHeaders:
    - name: Host
      value: localhost
  initialDelaySeconds: 5 # Delay before the first probe is executed
  periodSeconds: 5 # How often to perform the probe
© www.soinside.com 2019 - 2024. All rights reserved.