flask tornado nginx 部署

2019-08-20 11:06:24 浏览数 (1)

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;

代码语言:javascript复制
    #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项目

0 人点赞