WebFaction
Community site: login faq

I'm trying to serve static file only to authenticated users via Django.

I've set up my view to perform access permission check in Django, then redirect the user to download the static file, which is a static-only app served by Webfaction's shared nginx frontend. I use 'X-Accel-Redirect' header to do this.

For example:

# Django view
def download_file(request):
    if request.user.is_authenticated():
        response = HttpResponse()
        response['X-Accel-Redirect'] = '/protected_location/some_file.txt'
        return response

In order to complete this setup, I need to 1) set up redirect location, 2) block access to my static files directory except for users redirected by Django via 'X-Accel-Redirect'. I've read that this is done by marking the directory as 'internal' on nginx configuration.

I want to do something like the following (nginx configuration):

location /protected_location/ {
  internal;
  alias  /usr/local/documents;
}

Is there a way to accomplish this using Webfaction's shared nginx frontend? I have not found a place where I can add in my configuration setting.

asked 25 Apr '14, 03:03

conest
1313
accept rate: 0%


No, you can't do this using the front-end shared Nginx instance. This instance is configured for maximum speed, which means that it doesn't take custom configuration or parameters; rather, it just (1) serves static files, and (2) proxies HTTP requests to a back-end application for handling. This is a deliberate design decision; we want the front-end Nginx to be as fast as possible.

What you'll probably want is to configure a local Nginx instance as you've described, and run it on a Custom Application (listening on port). This will give you full access to the associated nginx.conf and should still be very fast.

Hope that helps!

permanent link

answered 25 Apr '14, 05:39

ryans ♦♦
5.0k93360
accept rate: 43%

thanks for a quick response. I've decided to use Apache .htaccess to control access instead of running local nginx instance just for this.

(25 Apr '14, 11:11) conest
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×909
×186
×125
×3

question asked: 25 Apr '14, 03:03

question was seen: 4,421 times

last updated: 25 Apr '14, 11:11

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM