简体   繁体   English

在子目录nginx + uwsgi上提供烧瓶app

[英]Serving flask app on subdirectory nginx + uwsgi

I am try to deploy flask on a sub directory on my website, this script is super light weight and doesn't need (actually it can't) to roll into the main project. 我尝试在我的网站上的子目录上部署烧瓶,这个脚本超重,并且不需要(实际上它不能)滚动到主项目。 How ever when I go to the end point, I get a 404 error from flask (can confirm that it is flask because the log shows activity). 然而,当我到达终点时,我从烧瓶中得到404错误(可以确认它是烧瓶,因为日志显示活动)。 I am passing uwsgi_param SCRIPT_NAME /upload; 我正在通过uwsgi_param SCRIPT_NAME /upload; and uwsgi_modifier1 30; uwsgi_modifier1 30; in my nginx config file, but that doesn't seem to work. 在我的nginx配置文件中,但似乎不起作用。 How can I get uwsgi to serve my flask application on an nginx sub location (subdir)? 如何让uwsgi在nginx子位置(subdir)上提供我的烧瓶应用程序?

Here is my nginx config (the /upload location is where the trouble is): 这是我的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;
    }
}

my uwsgi.ini file: 我的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

and finally my entire flask app: 最后我的整个烧瓶应用程序:

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

the point of this script is to upload images to my static server. 这个脚本的重点是将图像上传到我的静态服务器。 My actual application sits on a separate server and thats why this can't sit there. 我的实际应用程序位于一个单独的服务器上,这就是为什么它不能坐在那里。

Basically what i want is to be able to go to dev.api.xyz.com/upload/123/ and hit upload_file. 基本上我想要的是能够去dev.api.xyz.com/upload/123/并点击upload_file。 I'm expecting a 405 error in the browser, because it is restricted to POST. 我期待浏览器出现405错误,因为它仅限于POST。 But I am getting a 404 error. 但我收到404错误。 Here is a sample output from the flask/uwsgi log: 以下是来自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)

So flask is getting hit but the url matching is not working. 烧瓶被击中,但网址匹配不起作用。 Thanks in advance for your help. 在此先感谢您的帮助。

The best solution I've found so far would be using the mount option of uwsgi. 到目前为止,我发现的最佳解决方案是使用uwsgi的mount选项。 In your config add the line 在您的配置中添加该行

mount = /upload=<scriptname>

Solution courtesy of https://serverfault.com/questions/461946 解决方案由https://serverfault.com/questions/461946提供

Best and quick solution for me 为我提供最好,最快捷的解决方

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

Hope this can help you. 希望这可以帮到你。 cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM