Apo*_*los 5 python apache django x-sendfile
我使用Vhost通过apache服务我的django.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>
Run Code Online (Sandbox Code Playgroud)
我想限制媒体文件仅在特定的登录用户上提供.所以我遇到了XsendFile.如果我正确理解它的作用是什么,当你让django对你想要服务的媒体文件进行所有检查时,它将由Apache作为静态文件提供.如果我猜对了,那么程序就是以下
然后我可以使用`并且将正常工作,就像使用初始媒体文件URL一样.我理解正确吗?我的问题如下:
关于1.激活XSendFile.这应该在我的Vhost标签内的conf文件中完成吗?设置XsendFile足够吗?我应该删除媒体指令的Alias以及媒体文件的部分吗?我希望媒体文件只能由我的视图提供?
还有什么我应该知道的吗?
编辑:我的设置是
<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>
Run Code Online (Sandbox Code Playgroud)
我的urls.py
#for xsendmedia file serving
url(r'^media\/(?P<path>.*)$', 'customer.views.media_xsendfile'),
Run Code Online (Sandbox Code Playgroud)
和我的看法
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
Run Code Online (Sandbox Code Playgroud)
我的问题是某些媒体文件是正常共享的,有些则不是,并且会出现内部服务器错误
还要确保像这样在 Apache 配置文件中设置 XSendFilePath,
Run Code Online (Sandbox Code Playgroud)XSendFile on XSendFilePath "//path/to/files/on/disk" <Directory "//path/to/files/on/disk"> Order Deny,Allow Allow from all </Directory>
并在返回响应时将其包含在您的视图中:
响应['X-Sendfile'] = smart_str(file_path)
并回答您的问题:
| 归档时间: |
|
| 查看次数: |
2317 次 |
| 最近记录: |