How can I host multiple websites on a droplet using Nginx

June 25, 2014 6.2k views

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!

2 Answers

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

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
Have another answer? Share your knowledge.