Flask + UWSGI + NGinx =除了root

时间:2018-04-24 13:28:45

标签: ubuntu nginx flask uwsgi

我一直在研究和尝试数百个配置,没有任何东西。我只有2条路线," /"和" / search /"。该网站在/位置加载确定,但当我点击img执行搜索时,它会挂起并给出504。

如果我像python3 app.py那样直接运行服务器,它可以很好地工作。

如果我运行它uwsgi,uwsgi --socket 0.0.0.0:8080 --protocol = http -w run:app,它也可以正常工作,所以我的观点中的问题是nginx配置,但我不知道什么可能是错的(我是新手)

这是我的档案:

run.py

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

server.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/

请帮忙。

再次感谢

0 个答案:

没有答案