Back to WSGI_Servers

See Also 安装moinWeb.pyuWSGIsupervisorNginx

Green Unicorn

Gunicorn (Green Unicorn,绿色独角兽) 是一个WSGI服务器,用来支持Python应用。它是 Ruby 的 Unicorn 服务器的Python实现。Unicorn被设计成轻量级的、易于使用的、并使用许多UNIX特性(UNIX idioms)。Gunicorn不是被设计成面向互联网的 – 它被设计成运行于Nginx之后,缓存慢请求,以及关注其他重要的内容。在 Gunicorn help 能找到 Nginx + Gunicorn 的简单配置步骤。

1. Installation

http://docs.gunicorn.org/en/latest/install.html

$ mkdir gunicorn
$ cd gunicorn
$ virtualenv env
$ source env/bin/activate
(env) $ pip install gunicorn gevent

1.1. Deploy by Supervisor

http://docs.gunicorn.org/en/stable/deploy.html#supervisor

[program:gunicorn]
command=/path/to/gunicorn main:application -c /path/to/gunicorn.conf.py
directory=/path/to/project
user=nobody
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/gunicorn_log/access.log
stderr_logfile=/var/gunicorn_log/error.log

2. Quickstart

http://docs.gunicorn.org/en/stable/

2.1. Configuration Overview

http://docs.gunicorn.org/en/stable/configure.html

2.2. Integration with Nginx

Set up Nginx and uWSGI

# https://github.com/benoitc/gunicorn/blob/master/examples/nginx.conf

# ln -s /srv/www/moin/etc/nginx.conf /usr/local/etc/nginx/conf.d/moin.km.com.conf

upstream app_server {
        # fail_timeout=0 means we always retry an upstream even if it failed
        # to return a good HTTP response

        # for UNIX domain socket setups
        server unix:/srv/www/moin/var/moin.sock fail_timeout=0;

        # for a TCP configuration
        # server 192.168.0.7:8000 fail_timeout=0;
}

server {
        listen          80;
        server_name     km.com wiki.li3huo.com wiki.wuxiaohua.com;
        keepalive_timeout 5;

        access_log /srv/www/moin/var/nginx_log/access.log;
        error_log /srv/www/moin/var/nginx_log/error.log;


        location / {
                # checks for static file, if not found proxy to app
                try_files $uri @proxy_to_app;
        }

        location @proxy_to_app {
                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;
                index  Index;
         }

         # config for moin farm instance
         location /book {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header SCRIPT_NAME /book;
                proxy_redirect off;
                proxy_pass http://app_server;
                index  Index;
     }

        # cache moin static content
        location /moin_static {
                alias   /srv/www/moin/env/lib/python2.7/site-packages/MoinMoin/web/static/htdocs;
                index  index.html index.htm;
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /srv/www/html;
        }
}
nginx.conf

2.3. Auto run on Mac OS

Refer to supervisor

3. Integration

3.1. Web.py

3.2. MoinMoin

moin.py: called by GUnicorn from supervisor/gunicorn_config.py: config for gunicorn

gunicorn.ini: supervisor config

4. Reference


CategoryDev

MainWiki: gunicorn (last edited 2016-08-30 19:08:47 by twotwo)