## 前言 > (直接抄別人的...) 1. Nginx:非同步Web伺服器,可以作為反向代理、負載平衡器和 HTTP快取等的功能。簡單來說就是做到靜動分離提高效率。 2. WSGI (Web Server Gateway Interface):負責代理伺服器及Flask的溝通,可以當成一個協議或規範。 3. 簡言之,Flask是框架,Nginx是網路服務,WSGI負責溝通。  ```text! 1. Web 客戶端發出請求 2. Nginx 發現是動態請求,告訴 WSGI 3. WSGI 把請求發給 Flask,拿到結果後回傳 4. Nginx 將結果回傳客戶端 5. 呈現在客戶端瀏覽器上 ``` [REF] [Flask App加上WSGI及Nginx服務](https://medium.com/%E5%B7%A5%E7%A8%8B%E9%9A%A8%E5%AF%AB%E7%AD%86%E8%A8%98/flask-app-%E5%8A%A0%E4%B8%8A-wsgi-%E5%8F%8A-nginx-%E6%9C%8D%E5%8B%99-b8bdc60d1dc7) ### 自己的心得是~其實Nginx+Flask就可以直接跑了,不過未來面對想做大型系統應該還是需要多間接一層WSGI。 --- ## 安裝套件 ```text! # 安裝Nginx sudo apt install nginx # 安裝 python套件管理工具 sudo apt install python3-pip # 安裝 flask套件 sudo pip install flask # 安裝uWSGI套件 sudo pip install uwsgi ``` > 這邊要注意的就是安裝flask與uwsgi的方式,不能使用python3 -m pip install flask(uwsgi)的方式,系統會無法找到對應的指令。 --- ## 設置參數 * 範例的flask程式碼,命名為server.py。 ```python from flask import Flask app = Flask(__name__) @app.route("/") def test(): return "Holle World!" if __name__ == '__main__': app.run() ``` --- * 建立uwsgi設定檔案,命名為server.config.ini。 > 1. uWSGI支援的配置格式很多,像是yaml、JSON、XML及INI,這邊以INI為介紹。 > 2. uwsgi協議方式有三種分別是http、uwsgi與socket。 ```ini [uwsgi] wsgi-file = server.py # 主要運行的py檔案 callable = app # flask實例化後的命名 socket = :5000 # 使用uwsgi協議方式 processes = 4 threads = 2 master = true chmod-socket = 660 vacuum = true # 當服務器退出的時候自動清理環境,刪除unix socket文件和pid文件 die-on-term = true uid = www-data # 建議不要以root權限運行uWSGI,安全性你懂得 ``` **PS: server.py與server.config.ini放置在同一目錄。** --- * 編寫Nginx設定檔(/etc/nginx/sites-available/DOMAIN) ```nginx server{ listen 80; location /{ include uwsgi_params; uwsgi_pass 127.0.0.1:5000; } } ``` --- * 重新啟動Nginx服務 ```shell sudo systemctl restart nginx.service ``` * 執行uWSGI ```shell uwsgi --ini server.config.ini ``` --- ## 參考資料 1. [【Flask 教學】實作 GCP 部署 Flask + Nginx + uWSGI](https://www.maxlist.xyz/2020/06/17/flask-nginx-uwsgi-on-gcp/#2_%E5%BB%BA%E7%AB%8B_Flask_%E7%9A%84_mainpy) 2. [Flask + uWSGI + Nginx架設記錄](https://blog.twtnn.com/2021/09/flask-uwsgi-nginx.html) 3. [uWSGI](https://coder.tw/?p=6375) 4. [【Python 教學】uWSGI 配置參數講解](https://www.maxlist.xyz/2020/06/20/flask-uwsgi/) --- ## 題外話 ### 若前面掛上cloudflare的話,如何在flask後臺抓取用戶真實IP位址呢? * [Nginx-cloudflare-取得使用者真實ip](https://blog.kawai.moe/linux/nginx-cloudflare-%E5%8F%96%E5%BE%97%E4%BD%BF%E7%94%A8%E8%80%85%E7%9C%9F%E5%AF%A6ip/) * [Flask+uwsgi使用客户端真实IP地址](https://blog.csdn.net/this_is_id/article/details/125315228) ```ini! log-x-forwarded-for = true ``` ```python ipAddress = request.headers['X-Forwarded-For'] ``` --- ### 接收上傳檔案時,遇到HTTP 413(Request entity too large)? * [Nginx 踩坑 413 Request Entity Too Large](https://juejin.cn/post/6844904120457887751) * 修改設定檔(sudo nano /etc/nginx/sites-available/DOMAIN) ```nginx client_max_body_size 64M; client_body_buffer_size 128k; ```
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.