How to assign a subdomain to my Django project on Digital ocean Droplet?

December 3, 2019 146 views
Django

We already have a website called X.com. Now i am building a Django App on a Digital ocean Droplet. i am able to access it using the ip-address.
But we want to have it called reporting.X.com for sharing it with our users.
On my domain providers, I already added an A record like the below
Host- reporting
Type - A
Content - <ip-address> of digital ocean droplet

However, I keep getting the following message when i try to access reporting.X.com.

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.
2 Answers

Okay I have resolved the issue. So I will answer this for future reference of other people.

You need to check 2 important places.

  1. /etc/nginx/sites-enabled/<your_project>

If you want your django app with just the subdomain, your project file in sites-enabled should look like below.

server {
    listen 80;
    server_name <ip-address> .X.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/eleena/reporting_app;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/eleena/reporting_app.sock;
    }

The dot in front of X.com is not a typo. This basically says any sub domain for X.com.

  1. settings.py file in Django App

You have to set ALLOWED_HOSTS this way.
ALLOWED_HOSTS = ['ip-address', '.X.com']
Again X.com is preceded with a dot.

In the question, I already mentioned that I created an A record with following details.

Host- reporting
Type - A
Content - <ip-address> of digital ocean droplet

After this, restart your nginx server.
sudo systemctl restart nginx

Now, theoretically it should work. But if it doesn’t (as it didn’t for me), you have to shutdown and restart your droplet.

sudo shutdown -r now

My Django app started to work after this activity.
Hope it helps.

The good news is your A record is pointing to your Droplet and is resolving correctly.

I assume you have created a Nginx conf for your subdomain and set the server_name property to reporting.x.com?

Restart Nginx after setting your conf by running:

systemctl restart nginx

  • I resolved it.
    I set the server_name property to .X.com and not reporting.X.com. Same in Django settings.py.
    I had to shutdown and restart my droplet using sudo shutdown -r now.
    It works now.

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!