Try_files没有命中PHP(NginX配置)

Ben*_*rel 9 php nginx

下面是我的nginx.conf.

如果不存在的文件/index.php服务很好.

但是当我的URL /foo/bar => /foo/bar/index.php通过下载作为PHP源代码提供时.

有任何想法吗?

try_files $uri $uri/ $uri/index.php /index.php;

location ~ \.php$ { 
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*rel 3

解决方案是添加索引index.php

    index index.php

    try_files $uri $uri/ $uri/index.php /index.php;

    location ~ \.php$ { 
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
Run Code Online (Sandbox Code Playgroud)

  • 实际上,解决方案是使用命名位置作为“try_files”的最终参数。问题是内部重定向(即尝试检查是否有任何其他正则表达式,例如 PHP 处理正则表达式)仅针对最终后备参数发出。在您问题的配置中,对“/index.php”的请求甚至没有尝试匹配正则表达式。您的答案有效的原因是,如果访问“anything”,“index everything”将发出内部重定向,从而导致实际的 PHP 正则表达式匹配。 (7认同)
  • @MahmoudAl-Qudsi 你可以发布一个答案来展示如何使用命名位置 (2认同)