到目前为止,我一直在尝试保护 Django 的媒体文件,但没有成功!我只是想让它只有管理员用户可以访问媒体文件夹。这是我的 Nginx 文件。
server {
listen 80;
server_name xxxxxxxxxx;
location = /favicon.ico {access_log off; log_not_found off;}
location /static/ {
alias /home/{site-name}/static_cdn/;
}
location /media/ {
internal;
root /home/{site-name}/;
}
location / {
this is setup and working. Didn't include Code though
}
我的网址文件
urlpatterns = [
url(r'^media/', views.protectedMedia, name="protect_media"),
]
而我的看法
def protectedMedia(request):
if request.user.is_staff:
response = HttpResponse()
response['Content-Type'] = ''
response['X-Accel-Redirect'] = request.path
return response
else:
return HttpResponse(status=400)
这会产生 404 Not Found Nginx 错误。这里有什么明显的错误吗?谢谢!
顺便说一句,我尝试将 /media/ 添加到 Nginx 设置中根 URL 的末尾。