Gunicorn 'Green Unicorn'-Python WSGI HTTP Server for UNIX

请阅读uWSGI与uwsgi协议了解另外一个知识点

本文将演示一下绿色独角兽(Gunicorn)是怎么工作的,python app将采用flask框架来简单实现。

来自官网的解释:

Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX. It's a pre-fork worker model. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.

安装:

pip install gunicorn

应为是用Gunicorn服务器部署的,所以我们就不用manage.py文件了,需要额外写一个Gunicorn的入口文件,如wsgi.py:

from myapp import create_app
import myapp
import os

__all__ = (
    'app',
)


config_name = os.environ.get('APP_CFG') or 'default'

app = create_app(config_name)

if __name__ == '__main__':
    app.run()

具体的示范示例app可以去本人github下载

运行服务器:

gunicorn -w 4 -b 0.0.0.0:8080 wsgi:app
#-b ADDRESS, --bind ADDRESS
#-w INT, --workers INT
    #The number of worker processes for handling requests.

这里写图片描述

我们通过浏览器访问以下:
这里写图片描述

就这么简单服务正常跑起来了!

注意:

因为本应用采用的是flask的restful api设计和蓝图,在python2下运行正常,在python3上会报错,原因需进一步排查!

高级配置

App preloading
If you are constrained for memory or experiencing slow app boot time, you might want to consider enabling the preload option. This loads the application code before the worker processes are forked.

gunicorn -w 4 -b 0.0.0.0:8080 wsgi:app --preload

Worker timeouts
By default, Gunicorn gracefully restarts a worker if hasn’t completed any work within the last 30 seconds. If you expect your application to respond quickly to constant incoming flow of requests, try experimenting with a lower timeout configuration.

gunicorn -w 4 -b 0.0.0.0:8080 wsgi:app --timeout 10

Max request recycling
If your application suffers from memory leaks, you can configure Gunicorn to gracefully restart a worker after it has processed a given number of requests. This can be a convenient way to help limit the effects of the memory leak.

gunicorn -w 4 -b 0.0.0.0:8080 wsgi:app --max-requests 1200

more advanced setting seeGunicorn Settings

既然闲着没事干,干脆接着配个Nginx代理吧,至于为什么要这么做,中说纷纭,需要自己去实践

编译安装Nginx

比如说我们有一个安装包,执行以下命令简单安装一下:

 tar zxf nginx-1.12.1.tar.gz && cd nginx-1.12.1 && sed -i.bak 's/#define NGINX_VER          "nginx\/" NGINX_VERSION/#define NGINX_VER          "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module && make && make install

然后修改配置,主要修改内容如下:

server {
    listen 80;
    server_name example.org; # 这是HOST机器的外部域名,用地址也行

    location / {
        proxy_pass http://127.0.0.1:8080; # 这里是指向 gunicorn host 的服务地址
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

  }

校验一下配置是否正确:

[root@lockey-rhel72 flask-app]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

做了软连接然后启动服务

ln -s /usr/local/nginx/sbin/nginx /usr/bin/
nginx -t
nginx

然后查看一下进程是否正常:

[root@lockey-rhel72 flask-app]# ps -aux | grep nginx
root       5301  0.0  0.1  46016  1932 ?        Ss   06:37   0:00 nginx: master process nginx
nobody     5312  0.0  0.2  46452  2276 ?        S    06:39   0:00 nginx: worker process
root       5350  0.0  0.0 112664   968 pts/2    S+   08:00   0:00 grep --color=auto nginx

再次访问(因为做了代理,所以不再需要端口):
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值