HHH
By:
HHH

Setup two website using Django apps and Nginx. After enter site1.com it keeps showing content of site2.com.

January 23, 2015 2.8k views

After I type in my browser www.site2.com, it open page of www.site1.com.
I think my configuration for both nginx and gunicorn are fine.

etc/nginx/site-available/site1 - my default website www.site1.com

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 _;

    keepalive_timeout 5;

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

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

    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;
    }
}

etc/nginx/site-available/site2 - my new website www.site2.com

upstream app_server2 {
    server 127.0.0.1:9500 fail_timeout=0;
}

server {
    listen 80;
    listen [::]:80;

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

    client_max_body_size 4G;
    server_name www.site2.com site2.com;

    keepalive_timeout 5;

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

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/test-django/site2/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_server2;
    }
}

etc/init/site1.conf

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django

exec gunicorn \
    --name=site1\
    --pythonpath=site1 \
    --bind=0.0.0.0:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    site1.wsgi:application

etc/init/site2.conf

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django/test-django/

exec gunicorn \
    --name=site2\
    --pythonpath=site2 \
    --bind=127.0.0.1:9500 \
    --config /etc/gunicorn.d/gunicorn.py \
    site2.wsgi:application

I didn't forget about sudo service nginx restart and service site2 restart, also after nginx -t I don't have any erros.

4 Answers

Can you show us the unedited version of these configs? Everything seems fine.

Is server_name _; edited to hide your domains? If not, remember to set it to server_name www.site1.com; and server_name www.site2.com; respectively.

Check out this tutorial for more information on setting up Nginx server blocks:

by Justin Ellingwood
Nginx is an incredibly powerful web server and reverse proxy. In this guide, we'll discuss how to set up server blocks on Ubuntu 14.04 in order to server multiple domains from a single server.
  • Everything works fine and servername; was set by default when I created a droplet.

Hi everything is working fine. I forgot to update my post. The problem was that I messed up path in my project directory.
server_name_; was set by default when I created a droplet.

  • server_name_; will work for accessing a single site. It's mainly when using multiple "server blocks" for different domains (or subdomains) that it becomes important,. It's how Nginx knows how to direct the requests correctly.

Have another answer? Share your knowledge.