1. 程式人生 > >利用nginx代理uwsgi處理flask web應用

利用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/
膜拜大佬~~