Django - 了解 X-Sendfile
我一直在使用 Django 进行一些有关具有访问控制的文件下载的研究。我的目标是完全阻止对文件的访问,除非由特定用户访问。我读过,在使用 Django 时,X-Sendfile 是实现此目的的首选方法之一(基于其他 SO 问题等)。我对在 Django 中使用 X-Sendfile 的初步理解是:
- 用户请求 URI 来获取受保护的文件
- Django 应用程序根据 URL 决定返回哪个文件,并检查用户权限等。
- Django 应用程序返回带有“X-Sendfile”的 HTTP 响应' 标头设置为服务器的文件路径
- Web 服务器找到该文件并将其返回给请求者(我假设 Web 服务器也沿途删除了 'X-Sendfile' 标头
)直接从 Django 文件,X-Sendfile 似乎可能是实现受保护下载的更有效方法(因为我可以依靠 Nginx 来提供文件,而不是 Django),但给我留下了 2 个问题:
- 我对 X-Sendfile 的解释是最不抽象地正确吗?
- 假设我不提供正常的前端 HTTP 访问(例如 http ://www.example.com/downloads/secret-file.jpg)到文件存储的目录(即,不要将其保存在我的
public_html
目录中)?或者,精通技术的用户可以检查标头等并逆向工程访问文件(然后分发)的方法吗? - 性能上真的差别很大吗?我是否会因为直接从 Django 提供 150Mb 文件的 8b 分块下载而使我的应用程序服务器陷入瘫痪,或者这不是问题?我问的原因是,如果两个版本几乎相同,那么 Django 版本会更好,因为我能够使用 Python 执行操作,例如记录已完成的下载数量、统计下载带宽等。
提前致谢。
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:
- User requests URI to get a protected file
- Django app decides which file to return based on URL, and checks user permission, etc.
- Django app returns an HTTP Response with the 'X-Sendfile' header set to the server's file path
- The web server finds the file and returns it to the requester (I assume the webs server also strips out the 'X-Sendfile' header along the way)
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:
- Is my explanation of X-Sendfile at least abstractly correct?
- 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)? - 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.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住为 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.