Skip to content

Instantly share code, notes, and snippets.

@lxneng
Created October 12, 2011 02:05
Show Gist options
  • Save lxneng/1280031 to your computer and use it in GitHub Desktop.
Save lxneng/1280031 to your computer and use it in GitHub Desktop.
Serving Static File with Permissions using Nginx, Uwsgi
#file are located in /resources
#eg to open /resources/sample.pdf -> http://www.mysite.com/MyFiles/sample.pdf
#location configuration in nginx.conf
location ^~ /resources/ {
internal;
root /path/to/myapp;
}
--------------------------------------
# __init__.py
#add route for static files with permissions
config.add_route('resource_file', '/MyFiles/{filename}')
config.add_view(view='myapp.views.resource_file',
route_name='resource_file',
permission='special')
----------------------------------------------
# myapp.views
import os
from pyramid.response import Response
def resource_file(request):
_here = os.path.dirname(__file__)
filename = request.matchdict['filename']
response= Response(content_type='application/pdf', headers={'X-Accel-Redirect':'/resources/'+filename})
response.status='200 OK'
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment