Try_files 没有命中 PHP(NginX 配置)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12091386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:47:29  来源:igfitidea点击:

Try_files does not hit PHP ( NginX configuration)

phpnginx

提问by Benjamin Harel

Below is my nginx.conf.

下面是我的 nginx.conf。

In case of non existing files /index.phpis served fine.

在不存在的文件的情况下/index.php提供罚款。

But when my URL is /foo/bar => /foo/bar/index.phpis served as PHP source code via download.

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

Any ideas?

有任何想法吗?

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

采纳答案by Benjamin Harel

Solution was to add index index.php

解决方案是添加索引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;
    }

回答by amitchhajer

My config

我的配置

index index.html index.htm index.php;
location ~ \.php$ {
        fastcgi_pass unix:/tmp/php-fpm.sock;
        include fastcgi_params;
      }

reload nginx and fastcgi both

重新加载 nginx 和 fastcgi

回答by Ville

I also had this same issue. When I had the following in the PHP context (location ~ ^(.+\.php)(.*)$ { ...):

我也有同样的问题。当我在 PHP 上下文 ( location ~ ^(.+\.php)(.*)$ { ...) 中有以下内容时:

try_files $fastcgi_script_name =404;

.. it always returned 404. Finally I read the try_files docs, where it says:

.. 它总是返回 404。最后我读了try_files 文档,它说:

The path to a file is constructed from the file parameter according to the root and alias directives.

文件的路径是根据 root 和 alias 指令从 file 参数构造的。

I had only defined aliasfor each location block (and none in the PHP-FPM block, of course), but didn't have an overall rootset anywhere, so NGiNX didn't know where to look for a file. Once I set root for the server block, it started working.

我只alias为每个位置块定义了(当然在 PHP-FPM 块中没有定义),但没有root任何地方的整体设置,所以 NGiNX 不知道在哪里查找文件。一旦我为服务器块设置了 root,它就开始工作了。