ALLOWED_HOSTS and Django

Viewed 60666

I tried to launch a Django 1.11 project on production server. When I start the app I see the following error:

Invalid HTTP_HOST header: 'bla-bla-bla.bla-bla-vla.com'. You may need to add u'bla-bla-bla.bla-bla-vla.com' to ALLOWED_HOSTS**

But, host "bla-bla-bla.bla-bla-vla.com" has been added to ALLOWED_HOSTS in settings.py already!

I tried to switch DEBUG from False to True and back. It works fine, then.

What am I doing wrong?

6 Answers

I had the same issue make fixed using edit settings.py

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

Then Run

python manage.py runserver 0.0.0.0:8000

Also, if you have a service like gunicorn between your localhost and your nginx server or apache2 server. Remember to restart it too.

sudo systemctl restart gunicorn

Encountered the same error with NGINX. If one is developing & still testing with port 80 only (no 443 yet), one may need to temporarily deactivate any strict HTTPS settings right from django settings.py itself. e.g comment:

# #### strict https settings
# #### UNCOMMENT at production
# SECURE_REFERRER_POLICY = "same-origin"
# SECURE_BROWSER_XSS_FILTER = True
# SESSION_COOKIE_SECURE = True
# CSRF_COOKIE_SECURE = True
# CSRF_COOKIE_HTTPONLY = True
# SECURE_HSTS_SECONDS = 15780000  
# SECURE_CONTENT_TYPE_NOSNIFF = True
# SECURE_HSTS_INCLUDE_SUBDOMAINS = True
# SECURE_HSTS_PRELOAD = True
# SECURE_SSL_REDIRECT = True
# SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
#
# 
# #PREPEND_WWW = True
# #BASE_URL=["https://www.example.com"]

The error disappears. You can then uncomment back to strict HTTPS settings when the port 443 nginx server block settings are also done.

Related