sy
By:
sy

Nginx, Gunicorn v18, and 2 Django v1.7rc1 sites - all traffic is routed to one site.

August 10, 2014 1.3k views

I have the above config on a Debian droplet. I have a staging site (staging.flashcard101.com) and a live site (flashcard101.com). DNS for both are pointing to the droplet IP address. When I browse to the respective sites, some devices and browsers work fine, but some only show the live content at both domain names. It seems nginx does not direct the traffic correctly each time. Any idea what may be going on, or where I should look to diagnose? Thanks.

Here are my nginx configs:
live:

server {
    listen 80;
    server_name www.flashcard101.com;

    location /static {
        alias /home/fc/sites/www.flashcard101.com/static;
    }

    location /media {
        alias /home/fc/sites/www.flashcard101.com/media;
    }

    location / {
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/www.flashcard101.com.socket;
    }
}

Staging:

server {
    listen 80;
    server_name www.staging.flashcard101.com;

    location /static {
        alias /home/fc/sites/www.staging.flashcard101.com/static;
    }

    location /media {
        alias /home/fc/sites/www.staging.flashcard101.com/media;
    }

    location / {
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/www.staging.flashcard101.com.socket;
    }
}

Gunicorn confs:
Live:

description "Gunicorn server for www.flashcard101.com"

start on net-device-up
stop on shutdown

respawn

setuid fc
chdir /home/fc/sites/www.flashcard101.com/source

exec ../venv/bin/gunicorn \
    --bind unix:/tmp/www.flashcard101.com.socket \
    --access-logfile ../access.log \
    --error-logfile ../error.log \
    flf.wsgi:application

Staging:

description "Gunicorn server for www.staging.flashcard101.com"

start on net-device-up
stop on shutdown

respawn

setuid fc
chdir /home/fc/sites/www.staging.flashcard101.com/source

exec ../venv/bin/gunicorn \
    --bind unix:/tmp/www.staging.flashcard101.com.socket \
    --access-logfile ../access.log \
    --error-logfile ../error.log \
    flf.wsgi:application
2 Answers

I just realized it has to do with using www. or not as a prefix...

I found the answer here - http://nginx.org/en/docs/http/server_names.html

In the nginx/sites-available/ config files add both paths to the server_name:

server {
    listen       80;
    server_name  example.org  www.example.org;
    ...
}
Have another answer? Share your knowledge.