title: 502 Bad Gateway, Django, Nginx, Gunicorn, Postgresql, Virtualenv
body: <p>I have been on this for a week now. I've been trying to deploy a little test app on digital ocean and I have followed many tutorials like this one:<a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn" rel="nofollow">https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn</a></p>
  
  <p>I even tried the one click install tutorial:
  <a href="https://www.digitalocean.com/community/tutorials/how-to-use-the-django-one-click-install-image" rel="nofollow">https://www.digitalocean.com/community/tutorials/how-to-use-the-django-one-click-install-image</a></p>
  
  <p>And last night i tried this one which i found to be the most recent one:
  <a href="http://franklevering.me/blog/how-deploy-your-django-app-postgresql-nginx-and-gunicorn" rel="nofollow">http://franklevering.me/blog/how-deploy-your-django-app-postgresql-nginx-and-gunicorn</a></p>
  
  <p>when i go to my IP: <a href="http://188.166.62.146" rel="nofollow">http://188.166.62.146</a> I get a Welcome to nginx! page.
  And when i go to  <a href="http://188.166.62.146:8001" rel="nofollow">http://188.166.62.146:8001</a> i get a 400 Bad Request.</p>
  
  <p>I run the following command:</p>
  
  <pre><code>gunicorn --log-file=- blog.wsgi:application
  
  [2015-02-20 07:02:55 +0000] [6089] [INFO] Starting gunicorn 19.2.1
  [2015-02-20 07:02:55 +0000] [6089] [INFO] Listening at: http://127.0.0.1:8000 (6089)
  [2015-02-20 07:02:55 +0000] [6089] [INFO] Using worker: sync
  [2015-02-20 07:02:55 +0000] [6094] [INFO] Booting worker with pid: 6094
  </code></pre>
  
  <p>in /var/log/nginx i get this error repeated:</p>
  
  <pre><code>2015/02/20 03:07:27 [error] 4755#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 95.128.91.3, server: 188.166.62.146, request: "GET /favicon.ico HTTP/1.1", u$$.1", upstream: "http://127.0.0.1:8001/favicon.ico", host: "188.166.62.146"
  </code></pre>
  
  <p>This is my settings.py file:</p>
  
  <pre><code>DEBUG = TEMPLATE_DEBUG = False
  
  
  import os
  BASE_DIR = os.path.dirname(os.path.dirname(__file__))
  
  ALLOWED_HOSTS = []
  
  
  INSTALLED_APPS = (
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'posts',
  )
  
  MIDDLEWARE_CLASSES = (
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.middleware.common.CommonMiddleware',
      'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.middleware.clickjacking.XFrameOptionsMiddleware',
  )
  
  ROOT_URLCONF = 'blog.urls'
  
  WSGI_APPLICATION = 'blog.wsgi.application'
  
  DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'NAME': 'blog',
          'USER': 'myuser',
          'PASSWORD': 'mypassword',
      }
  }
  LANGUAGE_CODE = 'en-us'
  
  LANGUAGE_CODE = 'en-us'
  
  TIME_ZONE = 'Asia/Nicosia'
  
  USE_I18N = True
  
  USE_L10N = True
  
  USE_TZ = True
  
  STATIC_ROOT = '/opt/blogger/blog/static/'
  STATIC_URL = '/static/'
  
  TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
  </code></pre>
  
  <p>And here is my configuration file in etc/nginx/sites-available/blogger :</p>
  
  <pre><code>server {
      server_name project.com;
  
      access_log off;
  
      location /static/ {
          alias /opt/blogger/blog/static/;
      }
  
      location / {
          proxy_pass http://127.0.0.1:8001;
          proxy_set_header X-Forwarded-Host $server_name;
          proxy_set_header X-Real-IP $remote_addr;
          add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
      }
  }
  </code></pre>
  
  <p>I used the current command for binding gunicorn:</p>
  
  <pre><code>blog.wsgi:application --bind 188.166.62.146:8001
  </code></pre>
  
  <p>/opt/blogger/blog# gunicorn blog.wsgi:application --bind 127.0.0.1:8001</p>
  
  <pre><code>[2015-02-20 13:02:37 +0000] [7922] [INFO] Starting gunicorn 19.2.1
  [2015-02-20 13:02:37 +0000] [7922] [INFO] Listening at: http://127.0.0.1:8001 (7922)
  [2015-02-20 13:02:37 +0000] [7922] [INFO] Using worker: sync
  [2015-02-20 13:02:37 +0000] [7927] [INFO] Booting worker with pid: 7927
  ^Z
  [1]+  Stopped                 gunicorn blog.wsgi:application --bind 127.0.0.1:8001
  </code></pre>
  
  <p>/opt/blogger/blog# <code>bg</code></p>
  
  <pre><code>[1]+ gunicorn blog.wsgi:application --bind 127.0.0.1:8001
  </code></pre>