NGINX try_files不工作:500服务器错误

我试图使用try_files让我的服务器回落到不同的文件位置,但我不能得到它的工作。 相反,我得到一个500服务器错误。 这里是文件结构:

select>networking>存档

首先,我想检查网页,然后存档,如果文件不在网上。 例如:如果我想抓取test.mp4,首先我要查看/ opt / web / then / opt / web / archive

这是configuration:

http { server { listen 80; root /opt/web/; location / { try_files $uri/ /opt/web/archive/; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; # Custom headers and headers various browsers *should* be OK with but aren't add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; autoindex on; set $no_cache ""; if ($request_uri ~* \.mp4$) { set $no_cache "1"; } proxy_no_cache $no_cache; proxy_cache_bypass $no_cache; } } } 

想法?

 try_files $uri/ $uri/archive/; 

也不会工作

当用户请求http://www.example.com/test.mp4$urivariables将包含/test.mp4

现在,当你有try_files $uri/ /opt/web/archive/; 在你的configuration中,nginx会尝试这些文件:

 /opt/web/test.mp4/ /opt/web/archive/ 

所以,你应该使用:

 try_files $uri archive$uri; 

在你的configuration中。

另一个小问题是你使用if 。 而不是这个:

 set $no_cache ""; if ($request_uri ~* \.mp4$) { set $no_cache "1"; } proxy_no_cache $no_cache; proxy_cache_bypass $no_cache; 

我build议你使用这个:

 location ~ \.mp4$ { proxy_no_cache 1; proxy_cache_bypass 1; } 

有两个问题,第一,try_files是不正确的,导致redirect循环无休止地,直到它的错误:

* 2重写或内部redirect周期,而内部redirect到“/ 06000513 //档案//档案//档案//档案//档案//档案//档案//档案//档案//档案//档案/」,客户端:00.000.000.00,服务器:,请求:“GET / 06000513 / HTTP / 1.1”,主机:“myhost.com”

解决方法是使用try_files $uri /archive$uri =404; over try_files $uri/ /opt/web/archive/;

第二个问题是caching,因为我们closurescaching,在单独的位置查找文件时出现错误。 最简单的修复方法是删除:

  set $no_cache ""; if ($request_uri ~* \.mp4$) { set $no_cache "1"; } proxy_no_cache $no_cache; proxy_cache_bypass $no_cache; 

新的configuration:

 http { server { listen 80; root /opt/web/; location / { try_files $uri /archive$uri =404; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; # Custom headers and headers various browsers *should* be OK with but aren't add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; autoindex on; } } }