【问题标题】:XsendFile with apache and djangoXsendFile 与 apache 和 django
【发布时间】:2014-07-08 12:12:33
【问题描述】:

我的 django 由 apache 使用 Vhost 服务。 conf文件如下

WSGIPythonPath /srv/www/myproject/testproject/


<VirtualHost *:80>
    ServerAdmin admin@betarhombus.com
    ServerName www.betarhombus.com
    WSGIScriptAlias / /srv/www/testproject/testproject/testproject/wsgi.py
    <Directory /srv/www/testproject/testproject/testproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    Alias /static/ /srv/www/testproject/testproject/static/
    Alias /media/  /srv/www/testproject/testproject/media/

    <Directory /srv/www/testproject/testproject/static>
        Require all granted
    </Directory>
    <Directory /srv/www/testproject/testproject/media>
        Require all granted
    </Directory>

</VirtualHost>

我想限制媒体文件只提供给特定的登录用户。所以我遇到了 XsendFile。如果我理解正确,它的作用是让 django 对要提供的媒体文件进行所有检查,然后由 Apache 作为静态文件提供。所以如果我猜对了,程序如下

  1. 激活 XsendFile。
  2. 创建检查媒体文件权限等的视图并提供服务
  3. 与 urls.py 文件中的 url 关联

然后我可以使用 ` 并且会像使用初始媒体文件 url 一样正常工作。我理解正确吗?我的问题如下:

关于 1.激活 XSendFile。这应该在我的 Vhost 标签内的 conf 文件中完成吗?设置 XsendFile 是否足够?我应该删除媒体指令的别名以及媒体文件的部分吗?我希望媒体文件仅由我的视图提供?

还有什么需要注意的吗?

编辑:我的设置是

 <VirtualHost *:80>
    ServerAdmin admin@betarhombus.com
    ServerName www.betarhombus.com
    WSGIScriptAlias / /srv/www/testproject/testproject/testproject/wsgi.py
    XSendFile On
    XsendFilePath /srv/www/testproject/testproject/media/
    <Directory /srv/www/testproject/testproject/testproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    Alias /static/ /srv/www/testproject/testproject/static/
    <Directory /srv/www/testproject/testproject/static>
        Require all granted
    </Directory>
</VirtualHost>

我的 urls.py

#for xsendmedia file serving
url(r'^media\/(?P<path>.*)$', 'customer.views.media_xsendfile'),

我的看法

def media_xsendfile(request, path):  
    #here will be checked if user can access media
    response = HttpResponse()
    response['Content-Type']=''
    response['X-Sendfile']= smart_str(os.path.join(settings.MEDIA_ROOT, path))
    return response

我的问题是某些媒体文件可以正常共享,而有些则不能正常共享,并出现内部服务器错误

【问题讨论】:

  • some of the media files are shared normally and some are not 是什么意思?你能再解释一下吗?你在使用 staff_member_required 装饰器吗?

标签: python django apache x-sendfile


【解决方案1】:

还要确保像这样在 Apache 配置文件中设置 XSendFilePath,

XSendFile on
XSendFilePath "//path/to/files/on/disk"
<Directory "//path/to/files/on/disk">
    Order Deny,Allow
    Allow from all
</Directory>

并在返回响应时将其包含在您的视图中:

response['X-Sendfile'] = smart_str(file_path)

并回答您的问题:

  • 在 vhost 标签中激活 XSendFile
  • 我已经在上面写了视图中还需要做什么
  • 我不确定您是否应该删除媒体别名,日志文件应该会告诉您是否有问题

【讨论】:

  • 我应该设置什么作为 XSendFilePath 的值?如果我离开媒体别名,如果通过在 url 中输入路径,用户将无法访问媒体文件?例如 www.mydomain.com/media/path_to_mediafile
  • 我认为这是不可能的,除非你的 urls.py 中有 /media url。我已经更新了上面的答案来回答你的问题。
  • 如果我有任何帮助,请接受我的回答,或者请用您的解决方案回答问题。谢谢!
【解决方案2】:

如果有人在使用较新版本的 Apache (2.4) 时遇到同样的问题,这就是我最终设法使它工作的方法:

<VirtualHost *:80>
        XSendFile on
        
        Alias /static/ /mnt/mysite/static/
        Alias /media/ /mnt/mysite/media/

        <Directory /mnt/mysite/static>
                Require all granted
        </Directory>

        <Directory /mnt/mysite/media>
                Require all granted
        </Directory>

        <Directory /mnt/mysite/media/protected>
                Require all denied
        </Directory>
        XSendFilePath /mnt/mysite/media/protected

        <Directory /home/benbb96/mysite>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        WSGIDaemonProcess mysite python-home=/home/benbb96/mysite/venv python-path=/home/benbb96/mysite/
        WSGIScriptAlias / /home/benbb96/mysite/config/wsgi.py
</VirtualHost>

我在守护程序模式下使用 wsgi 运行我的网站(参见 docs),我想保护一些上传到 protected/ 中的媒体文件。所以我添加了一个&lt;Directory&gt; 指令来阻止对它的访问,然后我在我的视图中使用 XSendFile,它还验证用户是否有权访问该文件,以提供受保护的文件。

请注意,我的静态和媒体文件托管在网络 (/mnt/) 的已安装文件夹上,但这不会导致任何问题。

【讨论】:

    猜你喜欢
    • 2021-11-27
    • 1970-01-01
    • 2019-04-12
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    相关资源
    最近更新 更多