1. 程式人生 > >flask筆記:13:將Flask應用程式部署在nginx,tornado的簡單方法

flask筆記:13:將Flask應用程式部署在nginx,tornado的簡單方法

flask程式碼,main.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

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


tornado 程式碼,run.py:

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
#匯入flask專案
from main import app

http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)#對應flask的埠
IOLoop.instance().start()

#如果要開啟多程序模式用下面的程式碼,不過僅在linux下
# http_server = HTTPServer(WSGIContainer(app))
# http_server.bind(8888)
# http_server.start(0)
# IOLoop.instance().start()

修改nginx配置檔案,nginx.conf:

server {
        listen       9900; #預設是80,我改成了9900
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://localhost:5000; #新增這句,指向flask
        }


啟動 run.py 和 nginx,開啟瀏覽器:


輸入nginx的IP和埠指向了flask專案