gpt4 book ai didi

python - 带有 apache 和 django 的 XsendFile

转载 作者:行者123 更新时间:2023-12-03 20:26:00 26 4
gpt4 key购买 nike

我的 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 作为静态文件提供。所以如果我猜对了,程序是以下
  • 激活 XsendFile。
  • 创建检查媒体文件权限等并为其提供服务的 View
  • 与 urls.py 文件中的 url 关联

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

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

    还有什么我应该注意的吗?

    编辑:我的设置是
     <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>

    我的网址.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

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

    最佳答案

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

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


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

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



    并回答您的问题:
  • 激活 vhost 标签中的 XSendFile
  • 我已经在 View
  • 中写了其他需要做的事情
  • 我不确定您是否应该删除媒体别名,日志文件应该告诉您是否存在问题
  • 关于python - 带有 apache 和 django 的 XsendFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24631585/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com