【发布时间】:2018-02-02 23:32:37
【问题描述】:
到目前为止,我一直在尝试保护 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 错误。这里有什么明显的错误吗?谢谢!
顺便说一句,我尝试在 Nginx 设置中将 /media/ 添加到根 URL 的末尾。
【问题讨论】:
-
查看您的配置,问题似乎是此答案中确定的问题:stackoverflow.com/a/45774975/1081569。受保护视图的 URL 不能与 nginx 配置中的 URL 相同。
-
谢谢,@PauloAlmeida。我对您发送给我的链接进行了一些更改,并设法使其正常工作!
标签: django ubuntu nginx sendfile django-media