nginx x-accel-redirect protection of static files

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# in your nginx.conf put something like this
'''
location /protected {
                internal; # only the apache instance can access this url directly
                alias   /protected/files/path/;
        }
'''

# put something like this in your views.py and give it an entry in the urls.py

# no point in letting unauthenticated users into the view
# but this depends on your requirements
@login_required
def nginx_accel(request,id):
    '''
    default django view, where id is an argument that identifies
    the ressource to be protected
    '''
    allowed = False
    pFile = get_object_or_404(ProtectedFile,pk=id)

    # do your permission things here, and set allowed to True if applicable
    if allowed:
        response = HttpResponse()
        url = '/protected/protected.file' # this will obviously be different for every ressource
        # let nginx determine the correct content type 
        response['Content-Type']=""
        response['X-Accel-Redirect'] = url
        return response
    
    return HttpResponseForbidden()

More like this

  1. Custom mod_python AuthenHandler by aeby 6 years, 8 months ago
  2. Effective content caching for mass-load site using redirect feature by nnseva 2 years, 9 months ago
  3. X-Sendfile static file serve view by dokterbob 3 years, 6 months ago
  4. Apache X-sendfile with permissions checking by h0axify 2 years ago
  5. apache authentication via cookies by sean 7 years, 1 month ago

Comments

(Forgotten your password?)