gpt4 book ai didi

python - 在子目录 nginx + uwsgi 上服务 flask 应用程序

转载 作者:行者123 更新时间:2023-11-28 16:31:49 25 4
gpt4 key购买 nike

我正在尝试将 flask 部署到我网站的一个子目录中,这个脚本非常轻量级并且不需要(实际上它不能)进入主项目。然而当我到达终点时,我从 flask 收到 404 错误(可以确认它是 flask,因为日志显示事件)。我在我的 nginx 配置文件中传递了 uwsgi_param SCRIPT_NAME/upload;uwsgi_modifier1 30;,但这似乎不起作用。我怎样才能让 uwsgi 在 nginx 子位置(子目录)上为我的 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

最后是我的整个 Flask 应用程序:

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)

所以 flask 被击中了,但是 url 匹配不起作用。预先感谢您的帮助。

最佳答案

到目前为止,我发现的最佳解决方案是使用 uwsgi 的 mount 选项。在您的配置中添加行

mount = /upload=<scriptname>

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

关于python - 在子目录 nginx + uwsgi 上服务 flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31151116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com