What is fastcgi_index in nginx used for?

18.5k views Asked by At

On many sites can be found this nginx location block :

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000
    fastcgi_index index.php
    ...
}

Given the official documentation of fastcgi_index, it seems like it is used when requests end with /. However, it doesn't match the regular expression of the location block above? Am I missing something about the fastcgi_index directive?

1

There are 1 answers

0
chiborg On BEST ANSWER

You are right, if your nginx configuration (outside the location directive) has no index directive, then the location directive will never match and the fastcgi_index directive is useless.

If you have a line like this on your configuration

index index.php

then a request to / will create an internal redirect to /index.php, the location will match and fastcgi will be called. php-fpm will need a SCRIPT_FILENAME parameter that points to the file being executed. Normally, the configuration looks something like this:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

$fastcgi_script_name contains the name of the matched script, so fastcgi_index is ignored.

There is at least one instance where fastcgi_index is useful and used: when nginx and php-fpm are on different servers and nginx can't match the index.php file.