Nginx 作为缓存:位置扩展匹配

Nginx 作为缓存:位置扩展匹配

我想使用 nginx 容器作为代理缓存。我的目标是使用 docker swarm 设置 CDN,其中包含该容器的 N 个副本。

好吧,我有一个指向这个 URL 的 html 页面:

 172.17.0.1:9000/media/example-av.mpd

但总是有 404 http 响应。这是 nginx default.conf

proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_methods GET HEAD POST;
proxy_cache_valid 200 100m;
proxy_ignore_headers Set-Cookie;

server {
    listen       80;
    server_name  172.17.0.1;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}


    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

    location /demo/ {

    proxy_cache my_zone;
    add_header X-Proxy-Cache             $upstream_cache_status;
    proxy_set_header X-Real-IP           $remote_addr;
    proxy_set_header X-Forwarded-For     $remote_addr;

    proxy_set_header Host                $host;

    proxy_pass http://172.17.0.1:8080/shaka-player-master/demo/index1.html ;

    }

location  ^~  /media/ {

    proxy_cache my_zone;
    add_header X-Proxy-Cache             $upstream_cache_status;
    proxy_set_header X-Real-IP           $remote_addr;
    proxy_set_header X-Forwarded-For     $remote_addr;

    proxy_set_header Host                $host;

    proxy_pass http://172.17.0.1:8080/shaka-player-master/media/example-av.mpd ;

    }




location  ~ /.mpd {

    proxy_cache my_zone;
    add_header X-Proxy-Cache             $upstream_cache_status;
    proxy_set_header X-Real-IP           $remote_addr;
    proxy_set_header X-Forwarded-For     $remote_addr;

    proxy_set_header Host                $host;

    proxy_pass http://172.17.0.1:8080/shaka-player-master/media/example-av.mpd ;

    }


}

我尝试过扩展名 (.mpd) 匹配和前缀匹配,但总是出现 404 未找到。我也可以更改 URL,但由于 shaka-player(用于流媒体的 Google 库)的限制,我需要在 URL 末尾添加 .mpd 扩展名。

有人能帮我吗?

答案1

我已经通过这种方式解决了:在 html 页面中,我将指向此 URL:

172.17.0.1:9000/示例-av1.mpd

在 nginx 配置文件中,我写了以下行:

location =  /example-av1.mpd {
    add_header 'Access-Control-Allow-Origin' '*';

    proxy_cache my_zone;
    add_header X-Proxy-Cache             $upstream_cache_status;
    proxy_set_header X-Real-IP           $remote_addr;
    proxy_set_header X-Forwarded-For     $remote_addr;

    proxy_set_header Host                $host;

    proxy_pass http://172.17.0.1:8080/shaka-player-master/media/example-av1.mpd;
  }

相关内容