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!
Heya,
ALLOWED_HOSTS
in settings.py
Ensure your domain name is listed in ALLOWED_HOSTS
. It should look something like this:
ALLOWED_HOSTS = ['your-domain.com', 'www.your-domain.com']
If you’re using environment variables for production settings, make sure the domain name is correctly passed to the ALLOWED_HOSTS
variable.
For example, in settings.py
:
import os
ALLOWED_HOSTS = os.getenv('DJANGO_ALLOWED_HOSTS', '').split(',')
Check the code on your Droplet and make sure that settings.py is actually being updated. Also, what do you mean by build and deploy? Can you give more info about your workflow?
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
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.