部署django:gunicorn+supervisor+nginx


1、gunicorn 配置文件(我放在django项目文件夹内):

import gevent.monkey
gevent.monkey.patch_all()

import multiprocessing
bind = '10.70.75.191:8001'    # your ip addr
user = 'username'
worker_class = 'gevent'
workers = multiprocessing.cpu_count() * 2 + 1

2、supervisor配置文件

[program:mysite]

#gunicorn配置文件所在的目录
command=gunicorn mysite.wsgi-c /home/username/djangode/mysite/gunicorn.conf

directory=/home/username/djangode/mysite
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile_maxbytes=20MB
stdoiut_logfile_backups=20

#设置日志存放目录
stderr_logfile=/home/username/djangode/mysite/log/error.log
stdout_logfile=/home/username/djangode/mysite/log/out.log

3、设置开机自动启动supervisor,在/etc/rc.local 文件中加入:

/usr/local/bin/supervisord -c /etc/supervisord.conf

4、配置nginx

server {
    listen      80;
    server_name 10.70.75.191;
    charset     utf-8;
    access_log  /srv/djangode/log/access_log;
    error_log   /srv/djangode/log/error_log;

    client_max_body_size    75M;

    location /static {
        alias /home/username/djangode/mysite/static;
    }

    location /media {
        alias /home/username/djangode/mysite/media;
    }

    location / {
        proxy_pass  http://10.70.73.191:8001;
        proxy_set_header Host               $host;
        proxy_set_header X-Real-IP          $remote_addr;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
     }
}

在配置nginx时,出现 *1 connect() failed (111: Connection refused) while connecting to upstream的错误!原因是因为proxy_pass使用的是localhost的8001端口即 http://127.0.0.1:8001。后来终于找到解决办法了,需要使用http://10.70.75.191:8001也就是你自己的服务器的IP地址代替http://127.0.0.1:8001

ps:本文中的IP地址是作者随意写的,如与你的IP雷同,绝对纯属巧合,哈~.~

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器