簡體   English   中英

在子目錄nginx + uwsgi上提供燒瓶app

[英]Serving flask app on subdirectory nginx + uwsgi

我嘗試在我的網站上的子目錄上部署燒瓶,這個腳本超重,並且不需要(實際上它不能)滾動到主項目。 然而,當我到達終點時,我從燒瓶中得到404錯誤(可以確認它是燒瓶,因為日志顯示活動)。 我正在通過uwsgi_param SCRIPT_NAME /upload; uwsgi_modifier1 30; 在我的nginx配置文件中,但似乎不起作用。 如何讓uwsgi在nginx子位置(subdir)上提供我的燒瓶應用程序?

這是我的nginx配置(/ upload位置是麻煩的地方):

upstream django {
    server app0.api.xyz.com:9002;
}

server {
    listen      443;
    ssl on;
    ssl_certificate /etc/nginx/ssl/cert_chain.crt;
    ssl_certificate_key /etc/nginx/ssl/api_xyz.key;

    charset     utf-8;
    server_name dev.api.xyz.com;

    location / {
            uwsgi_pass django;
            include /etc/nginx/uwsgi_params;
    }

    location /media  {
            alias /var/xyzdata;
    }

    location /upload {
        include /etc/nginx/uwsgi_params;
        uwsgi_pass unix:/var/sockets/upload.sock;
        uwsgi_param SCRIPT_NAME /upload;
        uwsgi_modifier1 30;
    }
}

我的uwsgi.ini文件:

[uwsgi]
chdir           = /home/ubuntu/uploadFlask
module          = images
callable        = app
socket          = /var/sockets/upload.sock
master          = true
processes       = 10
vacuum          = true
uid             = www-data
gid             = www-data
daemonize       = /var/log/uploads/error.log

最后我的整個燒瓶應用程序:

import os
from flask import Flask, request, redirect, url_for,Response, jsonify
from werkzeug import secure_filename
import time

UPLOAD_FOLDER = '/var/xyzdata'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DEBUG'] = True

@app.route('/<user>', methods=['POST'])
def upload_file(user):
    file = request.files['file']
    if file:
            file_id = str(time.time()).replace('.','_')
            filename = "{0}/images/{1}.jpg".format(user, file_id)
            path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            d = os.path.dirname(path)
            if not os.path.exists(d):
                    os.makedirs(d)
            file.save(path)
            return jsonify(physical_path=path, virtual_path=filename,
                           id=file_id)

@app.route('/delete/<user>/<id>/', methods=['POST'])
def delete_file(user, id):
    pass

這個腳本的重點是將圖像上傳到我的靜態服務器。 我的實際應用程序位於一個單獨的服務器上,這就是為什么它不能坐在那里。

基本上我想要的是能夠去dev.api.xyz.com/upload/123/並點擊upload_file。 我期待瀏覽器出現405錯誤,因為它僅限於POST。 但我收到404錯誤。 以下是來自flask / uwsgi日志的示例輸出:

[pid: 27900|app: 0|req: 4/5] 50.199.33.84 () {40 vars in 669 bytes} [Wed Jul  1 01:03:51 2015] GET /upload/things@things.com => generated 233 bytes in 0 msecs (HTTP/1.1 404) 2 headers in 72 bytes (1 switches on core 0)

燒瓶被擊中,但網址匹配不起作用。 在此先感謝您的幫助。

到目前為止,我發現的最佳解決方案是使用uwsgi的mount選項。 在您的配置中添加該行

mount = /upload=<scriptname>

解決方案由https://serverfault.com/questions/461946提供

為我提供最好,最快捷的解決方

location ~ /upload(/.*) {
    include /etc/nginx/uwsgi_params;
    uwsgi_pass unix:/var/sockets/upload.sock;
    uwsgi_param PATH_INFO "$1";
}

希望這可以幫到你。 干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM