问题
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