使用Nginx X-accel-redirect和Django REST

时间:2016-10-29 07:25:28

标签: nginx django-rest-framework x-accel-redirect

我无法理解我错在哪里:如果我禁用Nginx位置,Django REST视图集会正确响应。如果我启用它,我会找不到Nginx 404。

nginx config

location /download {
    internal;
    alias /opt/myproject/media-protected;
}

Django REST视图集

class ServeProtectedFile(APIView):
authentication_classes = (authentication.TokenAuthentication,)

def get(self, request):
    filename = self.kwargs['filename']

    userfile = UserFile.objects.get(filename=filename)
    response = HttpResponse()
    response["Content-Disposition"] = "attachment; filename={0}".format(userfile.filename)
    response['X-Accel-Redirect'] = "/download/{0}".format(userfile.file.name)
    return response

urls.py

url(r'^download/(?P<filename>[^/]+)/$', ServeProtectedFile.as_view()),

0 个答案:

没有答案