Django DisallowedHost error

断了今生、忘了曾经 提交于 2021-02-18 12:26:53

问题


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/

In my settings.py I have:

ALLOWED_HOSTS = ['*']

This should allow any host shouldn't it?

Edit: 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.


回答1:


I finally found the solution to the problem.

The wsgi.py that's connecting django with apache was overriding the ALLOWED_HOSTS setting in my settings.py.

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.




回答2:


just add the url to ALLOWED_HOSTS:

ALLOWED_HOSTS = ['ip.ip.ip.ip', '.example.com', '127.0.0.1']



回答3:


I had the same issue, and I added the correct IP address in ALLOWED_HOSTS, I tried with * too and nothing worked, until I restarted the instance I was working on. Then it all worked fine.



来源:https://stackoverflow.com/questions/40565188/django-disallowedhost-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!