title: nginx + gunicorn 502 bad gateway
body: <p>I'm trying to deploy a django project on VPS server via nginx and gunicorn, but I got an nginx error '502 bad gateway' when I try to open my page <code>rollservice-rostov.ru</code></p>
  
  <p>Something is wrong with my Nginx configuration, but I can't figure out what it is.</p>
  
  <p><strong>/etc/nginx/sites-enabled/rollservice.conf:</strong></p>
  
  <pre><code>upstream rollservice-rostov.ru {
      server 127.0.0.1:8010 fail_timeout=0;
  }
  
  server {
      listen 80;
      server_name  www.rollservice-rostov.ru;
      rewrite ^/(.*) http://rollservice-rostov.ru/$1 permanent;
      client_max_body_size 4G;
      access_log /home/django/logs/rollservice/nginx.access.log;
      error_log /home/django/logs/rollservice/nginx.error.log;
  
      location /static/ {
          alias   /home/django/projects/rollservice/static/;
      }
  
      location / {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_redirect off;
  
          if (!-f $request_filename) {
              proxy_pass http://rollservice-rostov.ru;
              break;
          }
      }
  
      error_page 500 502 503 504 /500.html;
      location = /500.html {
          root /home/django/projects/rollservice/templates/;
      }
  }
  </code></pre>
  
  <p><strong>/etc/supervisor/conf.d/rollservice.conf:</strong></p>
  
  <pre><code>[program:rollservice]
  command=/home/django/projects/rollservice/run/run.sh
  directory=/home/django/projects/rollservice
  user=django
  autostart=true
  autorestart=true
  stderr_logfile=/home/django/logs/rollservice/gunicorn_errors.log
  stdout_logfile=/home/django/logs/rollservice/gunicorn_access.log
  </code></pre>
  
  <p><strong>/home/django/projects/rollservice/run/run.sh:</strong></p>
  
  <pre><code>#!/bin/bash
  
  NAME="rollservice"
  SRCDIR=rollservice
  HOMEDIR=/home/django
  
  PROJECTDIR=${HOMEDIR}/projects/${NAME}
  VIRTUALENV=${HOMEDIR}/.envs/${NAME}
  SOCKFILE=${HOMEDIR}/tmp/${NAME}.sock
  
  USER=django
  GROUP=django
  NUM_WORKERS=3
  DJANGO_SETTINGS=rollservice.settings
  DJANGO_WSGI_MODULE=rollservice.wsgi
  
  echo "Starting $NAME as `whoami`"
  
  # Activate the virtual environment
  cd $PROJECTDIR
  source ${VIRTUALENV}/bin/activate
  export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS
  export PYTHONPATH=$PROJECTDIR:$PYTHONPATH
  
  # Create the run directory if it doesn't exist
  RUNDIR=$(dirname $SOCKFILE)
  test -d $RUNDIR || mkdir -p $RUNDIR
  
  # Start your Django Unicorn
  # Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
  exec ${VIRTUALENV}/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
    --name $NAME \
    --workers $NUM_WORKERS \
    --user=$USER --group=$GROUP \
    --bind=127.0.0.1:8010 \
    --log-level=debug \
    --log-file=$HOMEDIR/logs/
  </code></pre>
  
  <p>Although, my page opens successfully via </p>
  
  <pre><code>lynx http://127.0.0.1:8010
  </code></pre>
  
  <p>This is last error logs:</p>
  
  <pre><code> rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
  2014/10/12 23:18:34 [error] 11425#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
  2014/10/12 23:29:02 [error] 11423#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
  2014/10/12 23:29:02 [error] 11423#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /modules/404.php HTTP/1.0", upstream: "http://185.20.226.216:81/modules/404.php", host: "rollservice-rostov.ru"
  2014/10/12 23:33:10 [error] 11905#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
  2014/10/12 23:33:12 [error] 11905#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 141.105.68.10, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
  [vps]rollservice&gt; sudo tail /var/log/nginx/error.log
  2014/10/12 23:13:27 [error] 11106#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
  2014/10/12 23:16:46 [error] 11295#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
  2014/10/12 23:18:34 [error] 11425#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
  2014/10/12 23:29:02 [error] 11423#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
  2014/10/12 23:29:02 [error] 11423#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /modules/404.php HTTP/1.0", upstream: "http://185.20.226.216:81/modules/404.php", host: "rollservice-rostov.ru"
  2014/10/12 23:33:10 [error] 11905#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
  2014/10/12 23:33:12 [error] 11905#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 141.105.68.10, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
  2014/10/12 23:35:22 [error] 11905#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 93.72.37.129, server: rollservice-rostov.ru, request: "GET / HTTP/1.1", upstream: "http://185.20.226.216:81/", host: "185.20.226.216"
  2014/10/12 23:35:22 [error] 11905#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 93.72.37.129, server: rollservice-rostov.ru, request: "GET /favicon.ico HTTP/1.1", upstream: "http://185.20.226.216:81/favicon.ico", host: "185.20.226.216"
  2014/10/12 23:35:22 [error] 11905#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 93.72.37.129, server: rollservice-rostov.ru, request: "GET /favicon.ico HTTP/1.1", upstream: "http://185.20.226.216:81/favicon.ico", host: "185.20.226.216"
  </code></pre>