大网数据广告活动
服务器运维问答

NGINX try_files not working: 500 Server Error

· 52CE 我爱测

I am trying to use try_files to get my server to fall back to a different file location, but I can't get it to work. Instead, I get a 500 Server Error. Here is the file structure: opt > web > archive First I want to check web, th…

问题原文

原帖链接 · Soatl · CC BY-SA 3.0

I am trying to use try_files to get my server to fall back to a different file location, but I can't get it to work. Instead, I get a 500 Server Error. Here is the file structure:

opt > web > archive

First I want to check web, then archive if the file is not in web. Ex: If I want to grab test.mp4, first I want to look in /opt/web/ then /opt/web/archive

Here is the config:

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;
    }

  }

}

Ideas?

try_files $uri/ $uri/archive/;

also will not work

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 3.0

When the user requests for http://www.example.com/test.mp4, the $uri variable will contain /test.mp4.

Now, when you have try_files $uri/ /opt/web/archive/; in your configuration, nginx will try these files:

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

So, you should use:

try_files $uri archive$uri;

in your configuration.

Another smaller issue is your use of if. Instead of this:

set $no_cache "";

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

proxy_no_cache $no_cache;
proxy_cache_bypass $no_cache;

I recommend that you use this:

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-12-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。