Flask部署入門:Gunicorn與Nginx配置教程
本文介紹了生產環境中部署Flask應用的方案:因Flask自帶開發服務器不支持高併發和不安全,需Gunicorn(WSGI服務器)和Nginx(反向代理)配合。 步驟如下:先安裝Gunicorn(`pip install gunicorn`)和Nginx(Ubuntu/Debian用`apt install nginx`);啓動Gunicorn時,用`gunicorn -w 4 -b 127.0.0.1:8000 app:app`(`-w`設worker數,`-b`綁定地址端口,`app:app`指定應用入口)。 接着配置Nginx,創建配置文件(如`flask_app`),設置反向代理到Gunicorn(`proxy_pass`),並處理靜態資源(`location /static`),啓用配置並重啓Nginx。 驗證時訪問`http://localhost`或用`curl`測試。進階可通過systemd配置Gunicorn開機自啓。常見問題包括端口占用、靜態文件路徑錯誤等,需檢查Gunicorn狀態或防火牆規則。 核心是通過Gunicorn運行應用、Nginx
閱讀全文