Gunicorn 'Green Unicorn'-Python WSGI HTTP Server for UNIX
2018-03-25 23:05
531 查看
请阅读uWSGI与uwsgi协议了解另外一个知识点
本文将演示一下绿色独角兽(Gunicorn)是怎么工作的,python app将采用flask框架来简单实现。
来自官网的解释:
安装:
应为是用Gunicorn服务器部署的,所以我们就不用manage.py文件了,需要额外写一个Gunicorn的入口文件,如wsgi.py:
具体的示范示例app可以去本人github下载
运行服务器:
我们通过浏览器访问以下:
就这么简单服务正常跑起来了!
注意:
高级配置
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.
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.
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.
more advanced setting seeGunicorn Settings
既然闲着没事干,干脆接着配个Nginx代理吧,至于为什么要这么做,中说纷纭,需要自己去实践
编译安装Nginx
比如说我们有一个安装包,执行以下命令简单安装一下:
然后修改配置,主要修改内容如下:
校验一下配置是否正确:
做了软连接然后启动服务
然后查看一下进程是否正常:
再次访问(因为做了代理,所以不再需要端口):
本文将演示一下绿色独角兽(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
再次访问(因为做了代理,所以不再需要端口):
相关文章推荐
- Python - 15 Line HTTP Server - Web Interface For Your Tools
- Apache httpd, an open-source HTTP server for modern operating systems including UNIX and Windows NT
- .NET/ASP.NET/C#/WCF/SQL Server/My SQL/Java/JSP/JDBC/Spring/Spring MVC/PHP/Python/Ruby/Shell/Agile/CSS/HTML/HTTP/Unix/Linux大量PDF书籍/电子书籍下载, Effective Java 下载
- My Annotation for HTTP Proxy server: PythonProxy
- Gunicorn 0.17 发布,Python HTTP 服务器
- linux下用python搭建简单的httpServer
- python client使用 http post 到server端
- Python的SimpleHTTPServer
- Python的SimpleHTTPServer
- python SimpleHTTPServer
- Simple HTTP Server and Client in Python
- Define a Custom Control Builder for a Custom Server Control(http://www.aspnetpro.com/newsletterarticle/2006/05/asp200605de_l/asp200605de_l.asp)
- Python SimpleHTTPServer
- python httpServer 传输图片
- Introduction to the Python Web Server Gateway Interface (WSGI)
- python http(通讯一个测试的例子Teraservertest)
- python -m SimpleHTTPServer
- python SimpleHTTPServer 2
- 使用python获得activemq信息(通过http://server:8161/
- [原创]用python检测LVS real server状态实现HTTP高可用