title: django project nginx: connect() failed (111: Connection refused) while connecting to upstream ubuntu server on Digital Ocean
body: <p>help me please, i use django + gunicorn + nginx and i get error
  nginx: connect() failed (111: Connection refused) while connecting to upstream
  I think the problem is a 9000 port? how to solve this problem?
  <a href="http://45.55.254.196/" rel="nofollow">502 Bad Gateway</a></p>
  
  <h1>/var/log/nginx/error.log</h1>
  
  <pre><code>2015/05/07 04:57:28 [error] 11439#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 5.178.184.108, server: 45.55.254.196, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:9000/favicon.ico", host: "45.55.254.196"
  </code></pre>
  
  <h1>sudo netstat -plntu</h1>
  
  <pre><code>Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      28846/nginx     
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      946/sshd        
  tcp6       0      0 :::80                   :::*                    LISTEN      28846/nginx     
  tcp6       0      0 :::22                   :::*                    LISTEN      946/sshd
  </code></pre>
  
  <h1>my config</h1>
  
  <h1>/etc/nginx/sites-available/hotel</h1>
  
  <pre><code>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 45.55.254.196;
  
      keepalive_timeout 5;
  
      # Your Django project's media files - amend as required
      location /media  {
          alias /home/django/hotel/media;
      }
  
      # your Django project's static files - amend as required
      location /static {
          alias /home/django/hotel/static;
      }
  
      # Proxy the static assests for the Django Admin panel
      location /static/admin {
         alias /usr/local/lib/python3.4/dist-packages/django/contrib/admin/static/admin/;
      }
  
      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;
      }
  }
  </code></pre>
  
  <h1>/etc/init/gunicorn.conf</h1>
  
  <pre><code>description "Gunicorn daemon for Django project"
  start on (local-filesystems and net-device-up IFACE=eth0)
  stop on runlevel [!12345]
  
  respawn
  
  setuid django
  setgid django
  chdir /home/django
  
  exec gunicorn \
      --name=hotel \
      --pythonpath=hotel \
      --bind=127.0.0.1:9000 \
      --config /etc/gunicorn.d/gunicorn.py \
      hotel.wsgi:application
  </code></pre>
  
  <h1>/etc/gunicorn.d/gunicorn.py</h1>
  
  <pre><code>from multiprocessing import cpu_count
  from os import environ
  
  def max_workers():
      return cpu_count() * 2 + 1
  
  max_requests = 1000
  worker_class = 'gevent'
  workers = max_workers()
  </code></pre>