I have recently host my django website on digital ocean droplet. I have set a domain name for it, and had the domain name in the setting ALLOWED_HOST variable, after push to the repo, build and deploy on digital ocean, I am getting the error Invalid HTTP_HOST header: ‘domain-name’. You may need to add ‘domain-name’ to ALLOWED_HOSTS. I check at the bottom of the debug page and find out that the value of ALLOWED_HOST doesn’t contain my domain-name, what can be the issue?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there!
There are a few things that you could check here starting with the ALLOWED_HOSTS
setting in settings.py
and making sure that it is correctly set with your domain name:
ALLOWED_HOSTS = ['domain-name.com', 'www.domain-name.com']
If you’re deploying with environment variables, make sure they are properly set and imported:
import os
ALLOWED_HOSTS = os.getenv('DJANGO_ALLOWED_HOSTS', 'localhost').split(',')
Check if the environment variable DJANGO_ALLOWED_HOSTS
is set in your droplet.
After that, if you use caching (e.g., with gunicorn
or uwsgi
), restart your Django server to apply the new settings:
sudo systemctl restart gunicorn
Or if you are using a Docker container, rebuild and restart it:
docker-compose down
docker-compose up --build -d
On another note, if DEBUG
is set to True
in production, it could cause unexpected behavior. Make sure to set it to False
:
DEBUG = False
If this is still not working, I would suggest checking your logs for more details:
tail -f /var/log/nginx/error.log
tail -f /var/log/gunicorn/error.log
Let me know how it goes!
- Bobby
Heya,
If your Allowed_host has not been updated it means that your Application has not picked up the new settings. For that to work, sometimes you nead to restart your application.
Heya, @b9f220d82ec74dcdbabcaff6fa4fde
If you are using a caching mechanism (Redis, Memcached), clear it to ensure stale configurations are not affecting your application.
Also you can ensure the deployed version of your settings.py
or its equivalent configuration file on the DigitalOcean droplet matches your local changes. Sometimes, deployment scripts might not push updates correctly.
Regards
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.