在子目录nginx + uwsgi上提供flask应用程序 [英] Serving flask app on subdirectory nginx + uwsgi
问题描述
uwsgi_param SCRIPT_NAME / upload;
和 uwsgi_modifier1 30;
,但这似乎不上班。我怎样才能得到uwsgi服务我的烧瓶应用程序在一个nginx子位置(subdir)? 这是我的nginx配置(/ upload位置是麻烦的地方):
upstream django {
server app0.api.xyz.com:9002;
}
服务器{
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;
位置/ {
uwsgi_pass django;
包含/ etc / nginx / uwsgi_params;
}
位置/媒体{
别名/ 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
最后我的整个应用程序:
导入os $ b $从瓶子导入烧瓶,请求,重定向,url_for,响应,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> ,方法= ['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)
如果不是os.path.exists(d):
os.makedirs d)
file.save(path)
return jsonify(physical_path = path,virtual_path = filename,
id = file_id)
@ app.route('/删除/< user> /< id> /',methods = ['POST'])
def delete_file(user,id):
pass
这个脚本的目的是将图像上传到我的静态服务器。我的实际应用程序坐在一个单独的服务器,这就是为什么这不能坐在那里。
基本上我想要的是能够去dev.api.xyz.com/upload/123/并点击upload_file。我期待在浏览器中发生405错误,因为它仅限于POST。但是我得到一个404错误。以下是flask / uwsgi日志的输出示例:
pre $ p $ 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 =>在0毫秒(HTTP / 1.1 404)中生成233个字节72个字节中的2个头(核心0上的1个开关)
所以烧瓶正在被击中,但URL匹配不起作用。感谢您的帮助。
迄今为止找到的最佳解决方案是使用 mount
选项的uwsgi。在你的配置中添加行
mount = / upload =< scriptname>
解决方案由 https://serverfault.com/questions/461946
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). I am passing uwsgi_param SCRIPT_NAME /upload;
and uwsgi_modifier1 30;
in my nginx config file, but that doesn't seem to work. How can I get uwsgi to serve my flask application on an nginx sub location (subdir)?
Here is my nginx config (the /upload location is where the trouble is):
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]
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. I'm expecting a 405 error in the browser, because it is restricted to POST. But I am getting a 404 error. Here is a sample output from the flask/uwsgi log:
[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. In your config add the line
mount = /upload=<scriptname>
Solution courtesy of https://serverfault.com/questions/461946
这篇关于在子目录nginx + uwsgi上提供flask应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!