Django Nginx X-Accel-Redirect for protected files on Webfaction

孤街醉人 提交于 2019-12-06 15:45:02

First, your location ^.*/protected-files is nonsense. I guess, you've missed ~ modifier, but even in that case it would be useless.

Second, you have not protected /protected/ folder. Direct request to /protected/some-file.pdf will download that file without any protection.

Third, you have /static/protected-files/some-file.pdf in X-Accel-Redirect, but you didn't mention any static folder before.

So, I would suggest following config:

server {
    listen       27796;
    server_name  myurl.com;
    root /home/ucwsri/webapps/static_media_ucwsri_nginx; 

    location / {
        autoindex on;
    }

    location ^~ /protected/ {
        internal;
    }

And django should be:

response = HttpResponse()
url = "/protected/some-file.pdf"
response['X-Accel-Redirect'] = url

return response

Summary:

  • Protect real folder.
  • X-Accel-Redirect is URI, just think about it as if user put that URI in browser address bar. The only difference is that internal will allow access with X-Accel-Redirect while forbid direct user access from browser.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!