ric vivio
ric vivio

Reputation: 11

django + ngnix, serving media files

i show you directly my code:

i handle the link ewayApp/media/{stuff} with django:

urls.py:

path('ewayApp/media/<str:media_url_1>/<str:media_url_2>/<str:media_url_3>',course.getMedia),

the handler:

course.py:

def getMedia(request, media_url_1, media_url_2, media_url_3):
    ...some stuff...     
    url = '/protected/' + media_url_1+'/'+media_url_2+'/'+media_url_3
    response['X-Accel-Redirect'] = url
    return response

and i'm sure going for the first link i pass through this function because i have some prints along the function.

my ngnix settings:

location ~ / {

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;


    proxy_pass http://127.0.0.1:8080;
}

location  ^~ /protected/ {
    internal;
    alias path/to/media/media/;
}

So, the django server runs on 127.0.0.1:8080 and ngnix proxies all the requests there. when accessing these media files if i go straight to the link https://website.com/ewayApp/media/{stuff} ngnix returns me back 404 error not found. I checked if the media are stored in path/to/media/media/, and they are, but obviously in subdirectories with media_url_1, media_url_1 ... that are specified in {stuff}

How to solve this?

Thanks in advance.

Edit:

Just to tell you that effectivly the link is right because if i turn the debug flag of django to True, the media files are displayed, so everything is stored in the right place and the link is right too, but somhow i can't display with this accel-redirect + ngnix

Solved:

as suggested by Mh Eftekhari i did something similar to what he answered but adding the '^~', my solution for ngnix setting:

location  ^~/protected/ {
    internal;

    alias /path/to/media/media;
    try_files $uri $uri/ =404;

}

location ~ / {

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;

    ##Use the domain.tld here.
    proxy_pass http://127.0.0.1:8080;
}

Upvotes: 1

Views: 49

Answers (1)

Mh Eftekhari
Mh Eftekhari

Reputation: 31

change your configuration to this

       location /protected{
                alias path/to/media/media/;
                try_files $uri $uri/ =404;
       }

Upvotes: 1

Related Questions