Question

Serve multiple Django applications with Nginx and Gunicorn using multiple domains

As I was following this tutorial on how to Serve multiple Django applications with Nginx and Gunicorn using multiple domains (https://www.youtube.com/watch?v=koo3bF2EPqk )

When accessing the site via my subdomain, my site is not working. I’m getting the following error

Invalid HTTP_HOST header: `'subdomain.example.com'`. You may need to add 'subdomain.example.com' to ALLOWED_HOSTS.

I have already added the same on allowed hosts and restarted nginix and Gunicorn.

I had another site which is already running with no issues. Droplet IP is being used for both domains.


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
June 28, 2023

Heya,

there are a couple of possibilities on the origin of the error,

You have not showed what you have inside your Django settings.py file, but I assume you have not configured the ALLOWED_HOSTS variable as you should have. This is how it should look like:

ALLOWED_HOSTS = [
    'subdomain.domain.com'
]

Also, make sure to restart the Nginx service after doing so. The settings file is loaded to memory, so if you make changes to it, you need to restart the server.

systemctl restart gunicorn and systemctl restart nginx

Bobby Iliev
Site Moderator
Site Moderator badge
June 28, 2023

Hi there,

It sounds like that your Nginx configuration might be the issue if it is serving the contents of only one of the applications.

Can you share both of the Nginx server blocks that you have for each of the two websites? You need to make sure that the reverse proxy rules for each of the sites is pointing to the correct web socket.

Also, can you verify that both of the Gunicorn services are up and running?

Would you mind also sharing the ALLOWED_HOSTS definition for each site as well?

Best,

Bobby

Thank you all, I was able to fix it by redirecting

www.subdomain.example.com to subdomain.example.com 

I used this tut here https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04#configure-nginx-redirect

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel