nginx + supervisrd + gunicorn + Flask

最近做的外包项目开发接近尾声,进入部署阶段,在选择技术的时候,查阅了不少资料。在此做备记。

几个特点:

1、nginx前后端服务器,upstream了4个端口。代码如下:

upstream huhuchen-site {
    server 127.0.0.1:5001;
    server 127.0.0.1:5002;
    server 127.0.0.1:5003;
    server 127.0.0.1:5004;
}

server_names_hash_bucket_size 64;

server {
    server_name huhuchen.com www.huhuchen.com;

    types_hash_max_size 2048;

    location / {

        proxy_set_header Host $host:80;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Via "nginx";

        client_max_body_size    10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout   60s;
        proxy_send_timeout      90s;
        proxy_read_timeout      90s;
        proxy_buffering         off;
        proxy_temp_file_write_size 64k;
        proxy_pass http://huhuchen-site;
        proxy_redirect          off;

    }

    location /static {
        root                    /home/huhuchen/huhuchen/;
        expires                 30d;
        add_header              Cache-Control public;
        access_log              off;
    }
}

相关资料:

upstream的几种配置方式

 

2、使用gunicorn启动flask服务,命令如下

gunicorn huhuchen:app -b localhost:50%(process_num)02d --log-level info --access-logfile /var/log/access-%(program_name)s-%(process_num)01d.log  

相关资料:

gunicorn 0.15.0 官方文档

running flask with gunicorn

 

遇到的问题:

a).15.0执行命令gunicorn huhuchen:app -b localhost:5000时,需要cd到应用huhuchen所在目录,不然会出错。

b).gunicorn需要配置日志文件,不然应用的访问日志将无法查看,可以直接在目录中配置日志路径,也可以配置在conf.py中,并在命令中导入。

 

3、使用supervisrd保证服务不死,配置如下:

[unix_http_server]
file=/tmp/supervisor.sock   ; (the path to the socket file)


[supervisord]
logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=500MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)
loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile=/var/log/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false               ; (start in foreground if true;default false)
minfds=1024                  ; (min. avail startup file descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

[program:ptour]
autostart=true                ; start at supervisord start (default: true)
command=/usr/local/bin/gunicorn huhuchen:app -b localhost:50%(process_num)02d --log-level info --access-logfile /var/log/access-%(program_name)s-%(process_num)01d.log             ; the program (relative uses PATH, can take args)
process_name=%(program_name)s-%(process_num)01d ; process_name expr (default %(program_name)s)
numprocs=5                    ; number of processes copies to start (def 1)
numprocs_start=0
redirect_stderr=true          ; redirect proc stderr to stdout (default false)
stdout_logfile=/var/log/%(program_name)s-%(process_num)01d.log        ; stdout log path, NONE for none; default AUTO
directory=/home/huhuchen               ; directory to cwd to before exec (def no cwd)
autorestart=true       ; whether/when to restart (default: unexpected)

 相关资料:

用supervisrd管理python进程

deploying flask with nginx

 

 

posted on 2012-10-31 23:19  huhuchen  阅读(1386)  评论(1编辑  收藏  举报

导航