Question

How can I host multiple websites on a droplet using Nginx

So, I have a Django droplet and I’ve pointed a subdomain (say django.example.com) to the ip address of my droplet.

Then, I installed ghost on the same droplet and now I want to have blog.example.com load my ghost installation. I followed this tutorial but when I opened blog.example.com, instead of ghost, django showed up.

So, can you please help me with setting up multiple "server"s on subdomains on Nginx on a droplet?

Thanks!


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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

@amin.bandali And your blog is dead? It was better if you added the ‘small gotchas’ here instead.

Hello there,

You can host multiple sites (domain names) on a single droplet, this can be achieved using virtual hosts.

The process is really simple, all you need to do is to spin up the droplet via the app marketplace and then manually add the virtual hosts for the other sites.

We have an article that covers the process, which you check here:

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-18-04

Hope that this helps! Regards, Alex

The key to setting up multiple websites on different domains or subdomains on the same server is setting up server_name in your server blocks. So your Ghost configuration would look something like:

server {
    listen 0.0.0.0:80;
    server_name blog.example.com;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:2368;
        proxy_redirect off;
    }
}

And your Django configuration would be like:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name django.example.com;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/django_project/django_project/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/django_project/django_project/static; 
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

And of course, remember to restart Nginx after making your changes:

sudo service nginx restart