部署 flask 应用到 nginx 和 tornado

在网上搜索了一下部署flask应用的方法,大部分是用uwsgi部署在nginx上面,部署了好久,都没有成功,多是我领悟能力太差,不过服务器上面的环境也够乱的有python2,python3,最后实在折腾得不行了,将uwsgi换成tornado,很是简单就搞定了,记录一下步骤,供之后参考: php

软件怎么安装就没必要说了,说说几个关键的地方: html

1。Flask的入口程序为run.py,代码以下: python

#coding=utf-8
#!/usr/bin/python

from somewhere import app  #somewhere 表示的包含Flask的实例,如app = Flask(__name__)

if __name__ == "__main__":
    app.run(debug=True)

2。在run.py的同级目录添加tornado应用程序tornado_server.py来托管run.py,代码以下: nginx

#coding=utf-8
#!/usr/bin/python

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from run import app

http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)  #flask默认的端口,可任意修改
IOLoop.instance().start()

3.若是直接运行python tornado_server.py会正常运行,输出以下: shell

* Running on http://127.0.0.1:5000/
 * Restarting with reloader

可是在生产环境中得部署到性能好的nginx上去,因此我简单测试了一下,下面把相应的nginx配置文件粘出来: flask

server {
        listen   80;
        server_name  abc.com;
        rewrite ^(.*) http://www.abc.com$1 permanent;
}

server{
        listen 80;

        #listen [::]:80 default_server;
        #access_log  /var/log/nginx/win2003_access.log  main;
        #include header_proxy.inc;

        server_name www.abc.com;

        #root   /var/www/abc;

        location / {
            #index  index.html index.htm index.php;
            #include uwsgi_params;
            #uwsgi_pass unix:/tmp/uwsgi.sock;
            proxy_pass http://localhost:5000;    #关键一点就是这里,意思是全部对http://www.abc.com:80的访问都会重定向到本机的5000端口上
        }

}

这里只是测试,别的一些优化好比静态文件暂没有让 nginx代理 服务器

配置好后只须要重载一下nginx 就能够生效: app

# nginx -s reload

看来tornado仍是很是不错的,一直知道它的性能优秀,之后有空得好好研究一下。 tornado

相关文章
相关标签/搜索