uWSGI 設定逐行解析
Ubuntu
48 views
Case Details
以下用精簡方式說明這份 uwsgi.ini 每一行的作用,方便快速理解與應用。
範例 example.uwsgi.ini
[uwsgi]
plugins = python3
master = true
# Listen via HTTP (matches Nginx proxy_pass)
http-socket = 127.0.0.1:8094
wsgi-file = /home/ai-tracks-rent-scooter/htdocs/rent-scooter.ai-tracks.com/wsgi.py
callable = application
chdir = /home/ai-tracks-rent-scooter/htdocs/rent-scooter.ai-tracks.com
virtualenv = /home/ai-tracks-rent-scooter/htdocs/rent-scooter.ai-tracks.com/.venv
processes = 8
threads = 2
enable-threads = true
buffer-size = 65535
reload-on-rss = 512
max-requests = 1000
max-requests-delta = 500
harakiri = 60
umask = 0022
uid = ai-tracks-rent-scooter
gid = ai-tracks-rent-scooter
die-on-term = true
vacuum = true
# Cheaper mode
cheaper = 2
cheaper-initial = 2
cheaper-step = 2
cheaper-algo = spare
基本設定
- plugins = python3:載入 Python3 plugin。
- master = true:啟用 master process,管理 workers。
HTTP 監聽
- http-socket = 127.0.0.1:8094:以 HTTP 方式提供服務,給 Nginx 的
proxy_pass使用。
專案與環境
- wsgi-file:指定 WSGI 入口檔(app 的進入點)。
- callable = application:WSGI 物件名稱。
- chdir:切換到專案資料夾。
- virtualenv:使用的 Python 虛擬環境。
Process / Thread
- processes = 8:最多 8 個 worker process(後面設定為主)。
- threads = 2:每個 process 開 2 個 threads。
- enable-threads = true:允許執行 thread。
記憶體與安全保護
- buffer-size = 65535:增加 HTTP header buffer。
- reload-on-rss = 512:worker 記憶體超過 512MB 自動重啟。
- max-requests = 1000:每處理 1000 個請求就重啟 worker。
- max-requests-delta = 500:重啟點加入隨機性避免同時重啟。
- harakiri = 60:請求超過 60 秒強制中斷。
權限與檔案
- umask = 0022:檔案預設權限模式。
- uid / gid:以指定使用者身份執行。
- die-on-term = true:收到 SIGTERM 時正常關閉。
- vacuum = true:關閉時清理 PID/socket 檔。
Cheaper Mode(動態調整 workers)
- cheaper = 2:最少啟動 2 個 worker。
- processes = 8:最多 8 個 worker。
- cheaper-initial = 2:起始 2 個 worker。
- cheaper-step = 2:每次增加 2 個 worker。
- cheaper-algo = spare:依需求動態增加/減少 worker。
這份設定已經非常適合一般 Python Web 專案(Flask / Django)在 Ubuntu + Nginx 的部署環境使用,既穩定又有記憶體保護與自動調整負載能力。
Project Information
- Ubuntu
- Published: 2025-11-13
- 48 views