nginx uwsgi flask 之间的关系
nginx 负责TCP/UDP连接、收发数据 ,把http请求通过uwsgi协议发送给uWSGI WEB服务器
uWSGI(pip install uwsgi) 负责解析HTTP协议,并将解析结果通过WSG协议传递给flask应用
flask web应用框架,负责业务代码(产生响应)
sequenceDiagram
webbrowser->>nginx:tcp/udp
nginx->>uwsgi:uWSGI(uwsgi)
uWSGI->>flask:call(env, request)
flask->>uWSGI:response(html)
uWSGI->>nginx:uwsgi(uwsgi)
nginx->>webbrowser:tcp/udp
部署实例
flask 配置
flask应用程序如下:
from flask import Flask
from flask import abort
from flask import redirect
from flask import make_response
from flask import render_template
app = Flask(__name__)
@app.route('/user/<name>')
def hello(name):#attention param name
if name != 'zhang':
abort(404) #
return '<h1>hello %s</h1>' % name
@app.route('/')
def index():
return redirect('http://www.baidu.com')
@app.route('/resp')
def resp():
response = make_response('<h1>this document carries a cookie</h1>')
response.set_cookie('answer', '42')
return response
@app.route('/hello/<name>')
def ext_hello(name):
return render_template('hello.html', name=name)
if __name__ == "__main__":
# app.run(host='0.0.0.0', port=8080)#flask demo can run independently,it has a simple web server
app.run()#use uwsgi for web server
uWSGI 配置
uWSGI + flask
[uwsgi]
http=172.16.16.62:8888
wsgi-file=/home/jimmy/ExtDisk2/learn_flask/flask_demo/hello.py
callable=app
pythonpath=/home/jimmy/ExtDisk2/learn_flask/flask_demo
nginx + uWSGI + flask
[uwsgi]
socket=172.16.16.62:8888
wsgi-file=/home/jimmy/ExtDisk2/learn_flask/flask_demo/hello.py
callable=app
pythonpath=/home/jimmy/ExtDisk2/learn_flask/flask_demo
uwsgi启动指令为: uwsgi --ini uwsgi.ini
nginx 配置
nginx默认配置为:/etc/nginx/nginx.conf
在/etc/nginx/nginx.conf配置文件中包含了/etc/nginx/sites-enabled/default
修改default文件如下:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
include uwsgi_params;
uwsgi_pass 172.16.16.62:8888;
}
location /image/{
root /home/jimmy/ExtDisk2/learn_flask/learn_nginx;
}
}
注意
location / {
include uwsgi_params;
uwsgi_pass 172.16.16.62:8888;
}
其他参考
https://blog.csdn.net/gymaisyl/article/details/85038932
https://blog.csdn.net/midion9/article/details/51354774