Permissões gerais para esses arquivos não são necessárias. O Nginx precisa apenas de uma permissão de leitura. Minha conta de usuário nginx é www-data
, gunicorn é executada com www_flask
privileges. O Nginx serve todos os arquivos enviados do frasco sem problemas.
Permissões de arquivo ls -lRr /webroot
/webroot:
total 4
drwxr-xr-x 3 www_flask www_flask 4096 Jul 4 13:20 gunicorn
/webroot/gunicorn:
total 4
drwxr-xr-x 2 www_flask www_flask 4096 Jul 4 14:57 uploads
/webroot/gunicorn/uploads:
total 924
-rw-rw-r-- 1 www_flask www_flask 164308 Jul 4 14:55 0001.jpg
-rw-rw-r-- 1 www_flask www_flask 53917 Jul 4 14:56 0004.jpg
-rw-rw-r-- 1 www_flask www_flask 349420 Jul 4 15:07 0005.jpg
-rw-rw-r-- 1 www_flask www_flask 365642 Jul 4 14:56 0006.jpg
configuração nginx
server {
listen 127.0.0.1:80;
server_name localhost;
location / {
try_files $uri @gunicorn_proxy;
}
location /media {
alias /webroot/gunicorn/uploads/;
}
location @gunicorn_proxy {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:8000;
}
}
Exemplo de upload de balão do tutorial /home/www_flask/evironments/flask/myapp/myapp.py
import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename
UPLOAD_FOLDER = '/webroot/gunicorn/uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect('media/' + filename)
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''
if __name__ == '__main__':
app.run(debug=True)