Question

Django Settings ALLOW_HOST not updating

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?


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
December 16, 2024

Heya,

Check 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?

Bobby Iliev
Site Moderator
Site Moderator badge
December 17, 2024

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

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.