Windows環境將Flask應用程式部署在tornado+nginx的簡單方法

來源:互聯網
上載者:User

標籤:flask tornado

1、Flask的入口程式為manage.py,代碼如下:

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

from somewhere import app  #somewhere 表示的包含Flask的執行個體,如app = Flask(__name__)if __name__ == "__main__":    app.run(debug=True)

2、在manage.py的同級目錄添加tornado應用程式tornado_server.py來託管manage.py,代碼如下:

#coding=utf-8#!/usr/bin/pythonfrom tornado.wsgi import WSGIContainerfrom tornado.httpserver import HTTPServerfrom tornado.ioloop import IOLoopfrom run import apphttp_server = HTTPServer(WSGIContainer(app))http_server.bind(5000, "0.0.0.0")  # 對外開啟訪問http_server.start(1)# http_server.listen(5000)  #或者只監聽127.0.0.1的預設連接埠IOLoop.instance().start()

3.如果直接運行python tornado_server.py會正常運行,輸出如下:

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

但是在生產環境中得部署到效能好的nginx上,nginx配置如下:

 

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 就可以生效:


Windows環境將Flask應用程式部署在tornado+nginx的簡單方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.