利用nginx代理uwsgi处理flask web应用

1,WIGS(Web Server Gateway Interface)服务器网关接口
它是用在 python web 框架编写的应用程序与后端服务器之间的规范, 是一个Web服务器(如nginx)与应用服务器(如uWSGI)通信的一种规范(协议)。

2,uWSGI
是一个Web服务器,实现WSGI协议,Http协议。把 HTTP 协议转化成语言支持的网络协议。比如把 HTTP 协议转化成 WSGI 协议,让 Python 可以直接使用。

3,nginx
Nginx是一个高性能的 Web 和反向代理服务器。Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。
这里写图片描述

4,flask
Flask 是一个 Python 实现的 Web 开发微框架。
这里写图片描述

安装:

pip install Flask
pip install uwsgi

代码实例:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

创建uwsgi配置文件:

[uwsgi]
http = 0.0.0.0:3000  # 监听的ip及端口
wsgi-file = 入口文件路径
callable = app 
processes = 4 # 进程数
threads = 2 # 线程数
pidfile = uwsgi.pid # 启动时的pid 文件
deamonize = 日志路径
buffer-size = 32678 
max-requests = 1000 # 最大请求数
master = true # 启动主进程

创建server.sh:

#!/bin/bash
export PYTHONIOENCODING=utf-8
export DEPLOY=dev
pwd
cd 项目路径
source venv/bin/activate # 使用虚拟环境
if [ ! -n "$1" ]
then
    echo "Usages: sh server.sh [start|stop|restart]"
    exit 0
fi

if [ $1 = start ]
then
    psid=`ps aux | grep "uwsgi.ini" | grep -v "grep" | wc -l`
    if [ $psid -gt 4 ]
    then
        echo "uwsgi is running!"
        exit 0
    else
        nohup uwsgi ./uwsgi.ini > /dev/null 2>&1 &
        echo "Start uwsgi service [OK]"
    fi

elif [ $1 = stop ];then
    uwsgi --stop uwsgi.pid
    echo "Stop uwsgi service [OK]"
elif [ $1 = restart ];then
    uwsgi --reload uwsgi.pid
    echo "Restart uwsgi service [OK]"
elif [ $1 = update ];then
    git pull
    echo "Update uwsgi service [OK]"
    uwsgi --reload uwsgi.pid
    echo "Restart uwsgi service [OK]"
else
    echo "Usages: sh server.sh [start|stop|restart|update]"
fi

启动服务:

sh server.sh start

重启服务:

sh server.sh restart

停止服务:

sh server.sh stop

配置nginx代理:

cd /etc/nginx/conf.d # 进入配置文件夹,在此文件夹中.conf格式的文件都会自动设置
新建.conf文件
server {
    listen       80 ; 
    access_log   /var/log/nginx/outer.log;

    # nginx配置
    # react build 静态文件
    # location /m/ {
        # root   /var/deploy/Mobile/dist;
        # index  index.html index.htm;
        ## url 切换时始终返回index.html
        # try_files $uri /index.html;
    # }

    # nginx配置
    location / {
        proxy_pass http://127.0.0.1:3000;
    }

    # 代理django api
    # location ~ "^/(api|auth|docs|swagger|redoc)/.*" {
        # include uwsgi_params;
        # uwsgi_pass 127.0.0.1:3001;
        # uwsgi_read_timeout 2;
    }

    # location /pystatic/ {
        # alias "/var/deploy/Outer/static/";
    #}
}

结果:

访问服务器80端口,出现Hello World!

参考网址:
https://blog.csdn.net/shu_8708/article/details/79068581
https://baijiahao.baidu.com/s?id=1590941335729952485&wfr=spider&for=pc
https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WSGIquickstart.html
https://blog.csdn.net/mnszmlcd/article/details/78819237
https://blog.csdn.net/kisscatforever/article/details/73129270
http://www.cnblogs.com/xiaoli3007/p/6090078.html
https://www.palletsprojects.com/p/flask/
膜拜大佬~~

猜你喜欢

转载自blog.csdn.net/qq_35790269/article/details/82115701
今日推荐