Created
October 23, 2011 03:20
-
-
Save jessejlt/1306827 to your computer and use it in GitHub Desktop.
nginx, flask, and file downloads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Okay so here's the setup: | |
[-] The primary server API is exposed via Flask (Python) and all static files, including all html, css, js is served by nginx. | |
[-] Python is exposing an API at url http://domain.com/api/download/<file_id>, where file_id is a database id for the file that we're interested in downloading. | |
1. User wants to download a file, so we spawn a new window with the url '/api/download/<file_id>' | |
2. Nginx intercepts the request, sees that it starts with /api/, and then forwards the request to Flask, which is being served on port 5000. | |
3. Flask routes the request to its download method, retrieves the pertinent data from the file_id, and constructs additional header settings to make nginx happy and to force the browser to see the file stream as a download request instead of the browser just trying to open the file in a new window. Flask then returns the modified header stream to nginx | |
4. Nginx is finally ready to do some work. While parsing the headers for the incoming request, it encounters "X-Accel-Redirect", which instructs nginx to serve the file specified | |
5. The browser should spawn a download dialog box, but instead it just spawns a new window that contains, as text, the binary contents of the file... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#download').click(function(e) { | |
var file_id = app.file_id; | |
if (file_id === undefined) { | |
return; | |
} | |
window.open('/api/download/' + file_id, 'Download'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://wiki.nginx.org/NginxXSendfile | |
location /download { | |
internal; | |
root /mnt/storage/; | |
} | |
location / { | |
proxy_set_header X-Sendfile-Type X-Accel-Redirect; | |
proxy_set_header X-Accel-Mapping /mnt/storage/=/download/; | |
} | |
# this is for Flask | |
location /api/ { | |
proxy_pass http://localhost:5000/; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import make_response | |
from server.database.api import get_file_params | |
@app.route('/api/download/<file_id>') | |
def download(file_id): | |
(file_basename, server_path, file_size) = get_file_params(file_id) | |
response = make_response() | |
response.headers['Content-Description'] = 'File Transfer' | |
response.headers['Cache-Control'] = 'no-cache' | |
response.headers['Content-Type'] = 'application/octet-stream' | |
response.headers['Content-Disposition'] = 'attachment; filename=%s' % file_basename | |
response.headers['Content-Length'] = file_size | |
response.headers['X-Accel-Redirect'] = server_path # nginx: http://wiki.nginx.org/NginxXSendfile | |
return response | |
you can use this extension for integrating your app into nginx X-Accel feature https://github.com/bapakode/Flask-Kaccel
and that extension above moved to https://github.com/lovapos/Flask-Kaccel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How are the
X-Accel-Mapping
andX-Sendfile-Type
headers handled by Flask?I mean Flask supports
X-Sendfile
out-of-the box but notX-Accel-Redirect
(hence your above gist).On my side I did quite a similar thing (with success) except that I did not set these
proxy_set_header
on nginx side. Why do you need to define them?