Django - 理解 X-Sendfile [英] Django - Understanding X-Sendfile

查看:22
本文介绍了Django - 理解 X-Sendfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Django 对具有访问控制的文件下载进行一些研究.我的目标是完全阻止对文件的访问,除非被特定用户访问.我读过在使用 Django 时,X-Sendfile 是实现这一目标的首选方法之一(基于其他 SO 问题等).我对在 Django 中使用 X-Sendfile 的基本理解是:

I've been doing some research regarding file downloads with access control, using Django. My goal is to completely block access to a file, except when accessed by a specific user. I've read that when using Django, X-Sendfile is one of the methods of choice for achieving this (based on other SO questions, etc). My rudimentary understanding of using X-Sendfile with Django is:

  1. 用户请求 URI 以获取受保护的文件
  2. Django 应用根据 URL 决定返回哪个文件,并检查用户权限等.
  3. Django 应用返回 HTTP 响应,其中X-Sendfile"标头设置为服务器的文件路径
  4. Web 服务器找到该文件并将其返回给请求者(我假设 Web 服务器也在此过程中删除了X-Sendfile"标头)

与直接从 Django 中删除文件相比,X-Sendfile 似乎是实现受保护下载的更有效方法(因为与 Django 相比,我可以依靠 Nginx 来提供文件),但给我留下了两个问题:

Compared with chucking the file directly from Django, X-Sendfile seems likely to be a more efficient method of achieving protected downloads (since I can rely on Nginx to serve files, vs Django), but leaves 2 questions for me:

  1. 我对 X-Sendfile 的解释至少在抽象上是正确的吗?
  2. 它真的安全吗,假设我不提供正常的前端 HTTP 访问(例如 http://www.example.com/downloads/secret-file.jpg) 到存储文件的目录(即,不要将它保存在我的 public_html目录)?或者,精通技术的用户能否检查标头等并逆向工程访问文件(然后分发)的方法?
  3. 性能上的差别真的很大吗?我是要通过直接从 Django 提供 8b 分块下载的 150Mb 文件来使我的应用程序服务器陷入困境,还是这不是问题?我问的原因是因为如果两个版本几乎相同,那么 Django 版本会更可取,因为我有能力用 Python 做事,比如记录已完成下载的数量、统计下载的带宽等.
  1. Is my explanation of X-Sendfile at least abstractly correct?
  2. Is it really secure, assuming I don't provide normal, front-end HTTP access (e.g. http://www.example.com/downloads/secret-file.jpg) to the directory that the file is stored (ie, don't keep it in my public_html directory)? Or, could a tech-savvy user examine headers, etc. and reverse engineer a way to access a file (to then distribute)?
  3. Is it really a big difference in performance. Am I going to bog my application server down by providing 8b chunked downloads of 150Mb files directly from Django, or is this sort-of a non-issue? The reason I ask is because if both versions are near equal, the Django version would be preferable due to my ability to do things in Python, like log the number of completed downloads, tally bandwidth of downloads etc.

提前致谢.

推荐答案

  1. 是的,这就是它的工作原理.
  2. 具体实现取决于网络服务器,但在 nginx 的情况下,建议将位置标记为内部位置以防止外部访问.
  3. Nginx 可以异步提供文件,而对于 Django,每个请求需要一个线程,这可能会导致更多并行请求的问题.

记住为 nginx 发送 X-Accel-Redirect 标头而不是 X-Sendfile.有关详细信息,请参阅 http://wiki.nginx.org/XSendfile.

Remember to send a X-Accel-Redirect header for nginx instead of X-Sendfile. See http://wiki.nginx.org/XSendfile for more information.

这篇关于Django - 理解 X-Sendfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆