[Django]-SIMPLEST way to set up password protection for a static site?

6👍

I use nginx and this is usually my solution. Maybe something similar to this is available for Apache. Don’t know.

In nginx, proxy can return a response with header X-Accel-Redirect with the location to where nginx is suppose to redirect. For example with the following nginx config:

server {
   ...
   location / {
       proxy_pass: ...;
   }
   location /media/secure/ {
       internal;
       alias ...;
   }
}

if the proxy pass will return:

X-Accel-Redirect: /media/secure/somefile;

Then nginx will redirect the request from the proxy to the /media/secure/ location, hence serving the static file.

So I usually authenticate a user in Django and then just return that header with the location to the file which then nginx serves. This is nice because then Django does not have to serve static file, users can’t just access that secure location (because of the internal directive) and also because if all mime types are configured in nginx, then all the files are served properly…

Again, this is for nginx, not for Apache, but maybe something similar is out there for Apache.

EDIT

Here is a simple Django code snippet assuming you are using same settings as above for nginx. This view serves secure files if its path is provided in the GET:

@login_required()
def serve_secure_static(request, file_root=os.path.join(settings.MEDIA_ROOT, 'secure')):
    if not request.method == 'GET':
        return HttpResponseBadRequest('Only GET allowed')

    if not 'file' in request.GET:
        return HttpResponseBadRequest('File query must be provided')

    # make sire loggen user is allowed to see the file
    # maybe check some custom permission

    file_path = request.GET['file']

    # if in DEBUG, make Django serve static file
    # because nginx might not be configured
    if settings.DEBUG:
        abs_file_path = os.path.join(file_root, file_path)
        file_data = open(abs_file_path, 'rb').read()
        return HttpResponse(file_data, mimetype=mimetypes.guess_type(file_path))

    # else make nginx serve static file
    else:
        redirect_url = '/%s' % file_path
        response = HttpResponse()
        response['X-Accel-Redirect'] = redirect_url
        return response

So then if you add this view to urlconfig:

url(r'^serve_secure_static/$', 'server_secure_static')

you can request secure files by:

http://domain.com/serve_secure_static/?file=path/to/file/here/relative/to/media/secure.css

which will serve the file from

/media/secure/path/to/file/here/relative/to/media/secure.css

Leave a comment