What is fastcgi_index for nginx?

On many sites you can find 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 looks like it is used when requests end in / . However, does this not match the regex of the location block above? Am I missing something in the fastcgi_index directive?

+6
source share
1 answer

You are correct if your nginx configuration (outside the location directive) does not have the index directive, then the location directive will never match, and the fastcgi_index directive fastcgi_index useless.

If you have such a line in your configuration

 index index.php 

then the request to / will create an internal redirect to /index.php , it will correspond to location and fastcgi. For php-fpm, you will need the SCRIPT_FILENAME parameter, which indicates the executable. Typically, the configuration looks something like this:

 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 

$fastcgi_script_name contains the name of the associated script, so fastcgi_index 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 cannot match index.php .

+3
source

Source: https://habr.com/ru/post/989001/


All Articles