Python 为什么 Django 会抛出错误“DisallowedHost at /”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40667519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:47:40  来源:igfitidea点击:

Why is Django throwing error "DisallowedHost at /"?

pythondjangodjango-settings

提问by alukin

I am setting up my own Django server using this Digital Ocean tutorial. I created the Django framework following each step, and ran the server using this command:

我正在使用这个Digital Ocean 教程设置我自己的 Django 服务器。我按照每个步骤创建了 Django 框架,并使用以下命令运行服务器:

./manage.py runserver 0.0.0.0:8000

When I tried to visit the IP at port 8000, the following error was shown:

当我尝试访问 8000 端口的 IP 时,显示以下错误:

DisallowedHost at /
Invalid HTTP_HOST header: 'XXX.XXX.XXX.XXX:8000'. You may need to add u'XXX.XXX.XXX.XXX' to ALLOWED_HOSTS.

(IP substituted with X's)

(IP 替换为 X)

Why is this happening?

为什么会这样?

回答by randyr

In your settings.py, there is a list called ALLOWED_HOSTS. You need to add the IP address you see in the error to that list:

在您的 中settings.py,有一个名为ALLOWED_HOSTS. 您需要将您在错误中看到的 IP 地址添加到该列表中:

ALLOWED_HOSTS = ['XX.XX.XX.XX']

ALLOWED_HOSTS = ['XX.XX.XX.XX']

The port should not be added.

不应添加端口。

Explanation:

解释:

Django checks the Hostheader of the HTTP request for a url/ip address that is within the allowed hosts.

Django 会检查HostHTTP 请求的标头,以查找允许主机内的 url/ip 地址。

From the django website:

从 Django 网站:

This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations.

这是一种防止 HTTP Host 标头攻击的安全措施,即使在许多看似安全的 Web 服务器配置下也是可能的。

https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts

https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts

回答by kapoc

you can include this configuration in the settings.py

您可以在 settings.py 中包含此配置

ALLOWED_HOSTS = ['*']

ALLOWED_HOSTS = ['*']

Important

重要的

Modify this configuration when you deploy your app in production enviroment

在生产环境中部署应用程序时修改此配置

回答by Vaibhav Savaliya

For Run Django Project on localhost with free hosting by "ngrok"

用于通过“ngrok”免费托管在本地主机上运行 Django 项目

run ngrok http 8000

运行 ngrok http 8000

(before run this in your project make sure your project are required to run on localhost like- python manage.py runserver)

(在您的项目中运行之前,请确保您的项目需要在 localhost 上运行,例如 python manage.py runserver)

http://563ae936.ngrok.io-> http://localhost:8000

http://563ae936.ngrok.io-> http://localhost:8000

Edit Setting.py

编辑设置.py

ALLOWED_HOSTS = ['563ae936.ngrok.io', 'localhost', '127.0.0.1', 'testserver']

ALLOWED_HOSTS = ['563ae936.ngrok.io', 'localhost', '127.0.0.1', 'testserver']

Here "563ae936.ngrok.io" Replace your Host name with removing http://or https://

这里“563ae936.ngrok.io”用删除http://https://替换您的主机名

回答by chad

Include both ('www.name.com', 'ip.ip.ip.ip') Set Debug = True, then retry the IP & URL Address.

包括两者 ('www.name.com', 'ip.ip.ip.ip') 设置 Debug = True,然后重试 IP 和 URL 地址。

Go to the Traceback section, find the message [ raise DisallowedHost(msg) ] click -> ▼ Local vars

转到 Traceback 部分,找到消息 [ raise DisallowedHost(msg) ] 单击 -> ▼ Local vars

It will show the incoming domain name and the settings for allowed hosts:

它将显示传入的域名和允许主机的设置:

*Variable       Value
*allowed_hosts  ['ip.ip.ip.ip', 'name.com']
*domain          'something.com'
*

Copy the incoming value into your settings.py. If the you see old settings restart the server\nginx

将传入值复制到 settings.py 中。如果您看到旧设置,请重新启动服务器\nginx

回答by Abhijeet Singh

Go to setting.py

转到设置.py

ALLOWED_HOSTS = ['*']

ALLOWED_HOSTS = ['*']