nginx强制提供本地文件,而不是Web URL

时间:2018-04-20 21:11:08

标签: nginx url-rewriting

我正在尝试对用户进行身份验证,然后提供所请求的文件。

网址:http://example.com/stream/asd.m3u8
静态文件:/stream/asd.m3u8(/ = Linux文件系统的根目录)

location ~ /stream/(.*)\.m3u8 {
   # authentication happens here, but commented out
     for debugging, and had the results below;


   alias /stream/;
   # 403 Forbidden (the directory and contents has chmod 777)

   rewrite /stream/(.*)\.m3u8 /stream/$1;
   # Infinite loop (curl: (47) Maximum (50) redirects followed)

   rewrite /stream/(.*)\.m3u8 /stream/$1 break;
   # 404 Not Found (in web root, that file doesn't exist)

   try_files /stream/$1.m3u8 =404;
   # 404 Not Found

   try_files /stream/$1.m3u8 =505;
   # 505
}


我认为nginx会将/stream/asd.m3u8视为网址 如何强制它将/stream/asd.m3u8视为本地文件?

另外,什么可能导致403 on alias指令?

谢谢!

1 个答案:

答案 0 :(得分:1)

URI与路径名相同,这意味着文档根目录为/。使用root指令设置文档根目录。有关详细信息,请参阅this document

为以/stream/开头的任何URI设置正确的文档根目录:

location /stream/ {
    root /;
}

如果您在此位置有其他文件,并且只想提供以.m3u8结尾的文件,则可以使用现有的正则表达式location块:

location ~ ^/stream/.*\.m3u8 {
    root /;
}

有关详情,请参阅this document

此处不需要alias指令,在这种情况下,首选root指令。要在正则表达式中使用alias,您必须捕获整个URI。有关详细信息,请参阅this document