Lets first take this minimal working example:
server {
listen 80;
server_name test.sc;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location /phpinfo {
alias /usr/share/phppgadmin/;
index phpinfo.php;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Here the problem is, that by using
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
FPM gets passed /usr/share/nginx/html/phpinfo.php for processing, since the with the "root" directive /usr/share/nginx/html was defined. This will be a "No input file specified." error.
How you could do it:
server {
listen 80;
server_name test.sc;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location /phpinfo {
alias /usr/share/phppgadmin/;
index phpinfo.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
This way, the php-files in the /usr/share/phppgadmin/ directory will be separately processed, you could even assign a different FPM-pool for those, in the context of /usr/share/phppgadmin/. Everything else will be processed using the defaults below. Nginx is limited to this behaviour because the first regex matching block will be used, it will not proceed to try any further or use multiple location blocks.
Now you can extend the configuration as you require:
server {
listen 80;
server_name test.sc;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location /phpinfo {
alias /usr/share/phppgadmin/;
index phpinfo.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location /phppgadmin {
alias /usr/share/phppgadmin;
index index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Additionally you can shorten this config by reusing certain parts:
/etc/nginx/php_settings:
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
/etc/nginx/sites-enabled/000.conf:
server {
listen 80;
server_name test.sc;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location /phpinfo {
alias /usr/share/phppgadmin/;
index phpinfo.php;
include php_settings;
}
location /phppgadmin {
alias /usr/share/phppgadmin;
index index.php;
include php_settings;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}