Using Nginx, Gunicorn, and Upstart to serve a WSGI app

This was a demo of getting the Django example app, polls, running in an Ubuntu VM hosted at DigitalOcean.

/etc/nginx/nginx.conf:

server {
    listen 80;      # listen on the standard port
    server_name _;  # normally, you'd set this to your domain name or IP address

    root /opt/apps/django-polls/;
    access_log /var/log/nginx/polls_access.log;
    error_log /var/log/nginx/polls_errors.log;

    charset   utf-8;
    keepalive_timeout  65;
    client_max_body_size  30M;

    location /static/ {  # have nginx serve static content
        access_log off;  # don't care about logging for static files
        expires 30d;     # in-browser caching
    }

    location / {  # pass requests along to gunicorn
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://127.0.0.1:8000/;  # so gunicorn can see the actual request IP address
    }
}

/etc/init/polls.conf (upstart file)

description "Django Polls Upstart Script"
start on runlevel [2345]
stop on runlevel [06]
kill timeout 300
respawn
respawn limit 10 5

script
  cd /opt/apps/django-polls
  . venv/bin/activate
  exec gunicorn -w 2 -b 127.0.0.1:8000 mysite.wsgi:application
  # -w 2: use two worker processes
  # -b 127.0.0.1:8000: bind address
  # mysite.wsgi: python path to wsgi server in django project
  # application: python variable in mysite.wsgi that points to Django app
end script