A
A
andrewshkovskii2012-12-11 10:47:10
Django
andrewshkovskii, 2012-12-11 10:47:10

Nginx+django problems serving private static files

There is such a task for the development process (not deploy yet):
When you click on the link, say 127.0 . before giving a response, you need to check that the user has access to the file (that is, that he could not access other people's files).
I'm looking at nginx and the X-Accel-Redirect response headers. At this stage, I do not want to use the built-in web server (runserver) in Jung, so that it would be easier to debug.
How to properly organize such access (gett url -> django view -> nginx static serve)? Via react-redirect?
While I'm trying to use this construction:
view:

def get(self, request, *args, **kwargs):
        audio_file = self.get_object()
        ogg_file_version = audio_file.audiofileversion_set.filter(format="ogg")
        if ogg_file_version.exists():
            ogg_file_version = ogg_file_version[0]
            res = HttpResponse()
            res["Content-type"] = "audio/ogg"
            res["X-Accel-Redirect"] = ogg_file_version.file.path
            res["Content-length"] = ogg_file_version.file.size
            return res
        return Http404()


nginx config:

server {

        listen 80;
        gzip off;
        expires off;

        location /static/ {
            add_header X-Static hit;
            autoindex on;
            expires off;
            root /Users/andrewshkovskii/workspace/ip_pbx/;
        }

        location / {
            proxy_pass       http://localhost:8000;
            rewrite ^/audiofiles/get/(\d+)/ /audiofiles/serve/$1/ last;
        }

        location /media/audio {
            internal;
            root   /var/ip_pbx/users;
        }
    }



But for now I'm getting 404. Which way to dig?
Thank you for your attention.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
andrewshkovskii, 2012-12-11
@andrewshkovskii

Solved by config

    server {

        listen 80;
        gzip off;
        expires off;
        error_log /usr/local/Cellar/nginx/1.2.5/logs/error.log debug;

        location /static/ {
            add_header X-Static hit;
            autoindex on;
            expires off;
            root /Users/andrewshkovskii/workspace/ip_pbx/;
        }

        location / {
            proxy_pass       http://localhost:8000;
            rewrite ^/audiofiles/get/(\d+)/ /audiofiles/serve/$1/ last;
        }

        location ~ /var/ip_pbx/users_andrewshkovskii/(\d+)/(audio|calls)/(.*) {
            internal;
            root   /;
        }
    }


Those. it was necessary to set the correct re for the file location.

M
MechanisM, 2012-12-11
@MechanisM

For such purposes, I somehow connected the django-sendfile application

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question