Gunicorn ‘Green Unicorn’ 是一個 UNIX 下的 WSGI HTTP 服務器
在 Gunicorn 上運行 Flask 應用
修改下面myproject爲自己的服務文件名即可
$ gunicorn myproject:app
使用如下命令即可啓動多個應用實例:
$ gunicorn -w 4 -b 127.0.0.1:4000 myproject:app
輸出如下內容代表服務創建成功:
[2020-02-11 14:50:24 +0800] [892] [INFO] Starting gunicorn 20.0.4
[2020-02-11 14:50:24 +0800] [892] [INFO] Listening at: http://0.0.0.0:5555 (892)
[2020-02-11 14:50:24 +0800] [892] [INFO] Using worker: sync
[2020-02-11 14:50:24 +0800] [895] [INFO] Booting worker with pid: 895
[2020-02-11 14:50:24 +0800] [896] [INFO] Booting worker with pid: 896
[2020-02-11 14:50:24 +0800] [898] [INFO] Booting worker with pid: 898
[2020-02-11 14:50:24 +0800] [899] [INFO] Booting worker with pid: 899
使用Nginx進行負載均衡
修改配置文件:修改服務端口的url即可
worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { server { listen 5556; # nginx端口 server_name localhost; location / { proxy_pass http://localhost:5555/run; # 服務端口的url } } }
從配置文件啓動:
sudo nginx -c nginx.conf