Django DisallowedHost错误 [英] Django DisallowedHost error
问题描述
我正在用apache运行django,并且在apache error.log中遇到以下错误:
I'm running django with apache and I'm getting the following error in my apache error.log:
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'example.com'. You may need to add 'example.com' to ALLOWED_HOSTS., referer: http://example.com/
在我的settings.py中,我有:
In my settings.py I have:
ALLOWED_HOSTS = ['*']
这应该允许任何主机吗?
This should allow any host shouldn't it?
经过更多调查后,我发现无论我将ALLOWED_HOSTS设置为什么,它始终会导致上述错误.使用服务器的本地IP地址时,可以正常访问网站.唯一不起作用的是远程URL.
After some more investigating I've found out that no matter what I set ALLOWED_HOSTS to, it always results in the above error. I can reach the website just fine when I'm using the local IP address of the server. The only thing that's not working is the remote URL.
推荐答案
我终于找到了解决问题的方法.
I finally found the solution to the problem.
将Django与apache连接的wsgi.py覆盖了我的settings.py中的ALLOWED_HOSTS设置.
The wsgi.py that's connecting django with apache was overriding the ALLOWED_HOSTS setting in my settings.py.
wsgi有自己的ALLOWED_HOSTS,可以独立于Django设置进行设置.检查所有可能的配置文件对于发现此错误至关重要.
wsgi has it's own ALLOWED_HOSTS that can be set independently from the django settings. Checking all the possible configuration files was crucial for finding this error.
这篇关于Django DisallowedHost错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!