首页 > 解决方案 > Flask + UWSGI + NGinx = 504 超时,除了 root

问题描述

我一直在研究和尝试数百种配置,但一无所获。我只有 2 条路线,“/”和“/search/”。该网站在 / 位置加载正常,但是当我单击 img 执行搜索时,它挂起并给出 504。

如果我像 python3 app.py 一样直接运行服务器,它可以完美运行。

如果我运行它 uwsgi, uwsgi --socket 0.0.0.0:8080 --protocol=http -w run:app ,它也可以完美运行,所以我认为问题出在 nginx 配置上,但我不知道可能发生什么可能是错的(我是新手)

这是我的文件:

运行.py

from server import app
if __name__ == '__main__':
      app.run()

服务器.py

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    uploaded_imgs = os.listdir('static/quick_demo')
    uploaded_imgs_rdn = set(random.sample(uploaded_imgs, 7))

    if request.method == 'POST':
        file = request.files['query_img']
        ts = int(time.time())
        img = Image.open(file.stream)  # PIL image
        uploaded_img_path = "static/uploaded/{}_{}".format(ts, file.filename)
        img.save(uploaded_img_path)

        //some code here ...
        return render_template('index.html',
                               query_path=uploaded_img_path,
                               query_upload_path=uploaded_imgs_rdn,
                               scores=scores)
    else:
        return render_template('index.html', query_upload_path=uploaded_imgs_rdn)


@app.route('/search/<img_id>', methods=['GET'])
def search_demo(img_id):
    img_name = img_id
    uploaded_imgs = os.listdir('static/quick_demo')
    uploaded_imgs_rdn = set(random.sample(uploaded_imgs, 7))

    if request.method == 'GET':
        # file = request.files['query_img']
        # ts = int(time.time())
        img = Image.open("static/quick_demo/{}".format(img_name))  # PIL image
        uploaded_img_path = "/static/quick_demo/{}".format(img_name)
        # img.save(uploaded_img_path)

        //some code here ...

        return render_template('index.html',
                               query_path=uploaded_img_path,
                               query_upload_path=uploaded_imgs_rdn,
                               scores=scores)
    else:
        return render_template('index.html', query_upload_path=uploaded_imgs_rdn)

nginx_site.conf

server {
        listen 80;
        listen [::]:80;

        root /var/www/site;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name XXX.XXX.XXX.XXX;
        location / {
                #try_files $uri $uri/ =404;
                include uwsgi_params;
                uwsgi_pass unix:/var/run/uwsgi/site.sock;
        }
}

/etc/uwsgi/vassals/site.ini

[uwsgi]
chdir = /var/www/site
module = run:app
touch-reload = /var/www/site/run.py
logto = /var/log/uwsgi/site.log

# master with 2 worker process (based on CPU number)
master = true
processes = 2

# use unix socket for integration with nginx
socket = /var/run/uwsgi/site.sock
chmod-socket = 660
# enable socket cleanup when process stop
vacuum = true

# ensure compatibility with init system
die-on-term = true

我正在关注本教程 https://code.luasoftware.com/tutorials/nginx/setup-nginx-and-uwsgi-for-flask-on-ubuntu/

请帮忙。

再次感谢

标签: ubuntunginxflaskuwsgi

解决方案


推荐阅读