Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is fastcgi_index in nginx used for?

Tags:

nginx

fastcgi

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?

like image 265
Gradient Avatar asked Jun 12 '15 11:06

Gradient


1 Answers

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.

like image 107
chiborg Avatar answered Nov 12 '22 21:11

chiborg