我正在尝试从 nginx 和 django 提供受保护的用户文件。
nginx.conf:
server {
listen 80;
gzip off;
expires off;
location /static/ {
add_header X-Static hit;
autoindex on;
expires off;
root /Users/andrewshkovskii/workspace/ip_pbx/;
}
location / {
proxy_pass http://localhost:8000;
rewrite ^/audiofiles/get/(\d+)/ /audiofiles/serve/$1/ last;
}
location /media/audio {
internal;
root /var/ip_pbx/users;
}
}
Django视图:
def get(self, request, *args, **kwargs):
audio_file = self.get_object()
ogg_file_version = audio_file.audiofileversion_set.filter(format="ogg")
if ogg_file_version.exists():
ogg_file_version = ogg_file_version[0]
res = HttpResponse()
res["Content-type"] = "audio/ogg"
res["X-Accel-Redirect"] = ogg_file_version.file.path
res["Content-length"] = ogg_file_version.file.size
return res
return Http404()
如果我尝试 GET ,比如说 localhost/audiofiles/get/74/ ,nginx 会将其重写为查看(/audiofiles/serve/74/),并且查看将返回响应,但它会在返回时响应,将尝试GET localhost:8000/%full_file_path% .. 为什么?(文件已存在..)