2

我正在尝试从 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% .. 为什么?(文件已存在..)

4

1 回答 1

0

由配置出售:

 server {

    listen 80;
    gzip off;
    expires off;
    error_log /usr/local/Cellar/nginx/1.2.5/logs/error.log debug;

    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 ~ /var/ip_pbx/users_andrewshkovskii/(\d+)/(audio|calls)/(.*) {
        internal;
        root   /;
    }
}

注意私有文件的位置(在我的情况下..)

于 2012-12-11T12:09:15.250 回答