django project nginx problem on ubuntu server

May 7, 2015 1.7k views
Deployment Django Python Nginx Networking Ubuntu

help me please, i use django + gunicorn + nginx and i get error
nginx: connect() failed (111: Connection refused) while connecting to upstream
how to correct it?

502 Bad Gateway

/var/log/nginx/error.log

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"

sudo netstat -plntu

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  

i have config

/etc/nginx/sites-available/hotel

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;
    }
}

/etc/init/gunicorn.conf

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

/etc/gunicorn.d/gunicorn.py

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()
2 comments
  • I think the problem is a 9000 port? how to solve this problem?

  • You should check the gunicorn log. You can change the log level to debg and try. Use--error-logfile <log-file-path> and --log-level debug options.

Be the first one to answer this question.