nginx+uwsgi+flask

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值