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;
    }
}
原文地址:https://www.cnblogs.com/slim-liu/p/9950339.html