1. 程式人生 > >nginx 代理flask應用的uwsgi配置

nginx 代理flask應用的uwsgi配置

socket代理配置:

關於uwsgi的用法,請自行百度,這裡只針對socket檔案和埠的不同,進行單一的記錄。

這種方式啟動的flask應用,由於是通過socket與nginx通訊的,所以必須制定socket檔案,無法進行叢集部署:

uwsgi配置

[uwsgi]
#application's base folder
base = /data/www/myproject

#python module to import
app = manage
module = %(app)

home = %(base)/venv
pythonpath = %(base)

#socket file
's location socket = /data/www/myproject/%n.sock #permissions for the socket file chmod-socket = 666 #the variable that holds a flask application inside the module imported at line #6 callable = app #location of log files logto = /data/www/log/uwsgi/%n.log

nginx配置:

server {
    listen      80;
    server_name localhost;
    charset     utf
-8; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass unix:/data/www/myproject/myproject_uwsgi.sock; } }

socket埠配置:

如果需要使用叢集,需要使用埠來與nginx進行通訊,那麼uwsgi的配置就會有些許的差異:

uwsgi配置

[uwsgi]
#application's base folder
base = /data/www/myproject

#python module to import
app 
= hello module = %(app) home = %(base)/venv pythonpath = %(base) #socket file's location socket = :8080 #socket = /data/www/myproject/%n.sock #permissions for the socket file chmod-socket = 644 #the variable that holds a flask application inside the module imported at line #6 callable = app #location of log files logto = /data/www/log/uwsgi/%n.log

差異就是將sock檔案,改為埠,當然也可以寫成如下格式:

socket = 0.0.0.0:8080
socket = 127.0.0.1:8080

那麼如果nginx需要負載兩臺甚至多臺server,就需要修改配置:

upstream ai-server {
        server server1:8080;
        server server2:8080;
}
server {
    listen      80;
    server_name localhost;
    charset     utf-8;
    client_max_body_size 75M;

    location / {
        include uwsgi_params;
        uwsgi_pass ai-server;
    }
}