加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

python-在子目录nginx uwsgi上提供Flask应用

发布时间:2020-12-13 20:54:30 所属栏目:Nginx 来源:网络整理
导读:我试图将flask部署在我网站的子目录中,此脚本重量极轻,不需要(实际上不需要)滚动到主项目中.但是,无论何时到达终点,我都会从flask中收到404错误(由于日志显示活动,因此可以确认它是flask).我正在传递uwsgi_param SCRIPT_NAME / upload;和uwsgi_modifier1 30;

我试图将flask部署在我网站的子目录中,此脚本重量极轻,不需要(实际上不需要)滚动到主项目中.但是,无论何时到达终点,我都会从flask中收到404错误(由于日志显示活动,因此可以确认它是flask).我正在传递uwsgi_param SCRIPT_NAME / upload;和uwsgi_modifier1 30;在我的nginx配置文件中,但这似乎不起作用.如何让uwsgi在Nginx子位置(subdir)上为Flask应用程序提供服务?

这是我的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提供

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读