什么是nginx中的fastcgi_index用于?

Gra*_*ent 10 fastcgi nginx

在许多网站上都可以找到这个nginx location块:

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000
    fastcgi_index index.php
    ...
}
Run Code Online (Sandbox Code Playgroud)

鉴于官方文档fastcgi_index,好像当请求,结束于使用/.但是,它与location上面的块的正则表达式不匹配?我错过了关于该fastcgi_index指令的一些内容吗?

chi*_*org 7

你是对的,如果你的nginx配置(在location指令之外)没有index指令,那么location指令永远不会匹配,fastcgi_index指令也没用.

如果您的配置中有这样的行

index index.php
Run Code Online (Sandbox Code Playgroud)

然后一个请求/将创建一个内部重定向/index.php,location将匹配和fastcgi将被调用.php-fpm将需要一个SCRIPT_FILENAME指向正在执行的文件的参数.通常,配置看起来像这样:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Run Code Online (Sandbox Code Playgroud)

$fastcgi_script_name包含匹配脚本的名称,因此fastcgi_index被忽略.

至少有一个实例fastcgi_index是有用和使用的:当nginx和php-fpm在不同的服务器上时,nginx与index.php文件不匹配.