问题
I configured nginx stable (1.4.4) + PHP (using FastCGI, php-fpm) on Debian. That works fine:
location ~* ^/~(.+?)(/.*\.php)$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
alias /home/$1/public_html$2;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_index index.php;
autoindex on;
}
I use the PATH_INFO variable, therefore I added the following line to fastcgi_params:
fastcgi_param PATH_INFO $fastcgi_path_info;
And in /etc/php5/fpm/php.ini:
cgi.fix_pathinfo = 0
I think that should work, but when I print out all server variables, PATH_INFO is always empty:
array (
'USER' => 'www-data',
'HOME' => '/var/www',
'FCGI_ROLE' => 'RESPONDER',
'QUERY_STRING' => '',
'REQUEST_METHOD' => 'GET',
'CONTENT_TYPE' => '',
'CONTENT_LENGTH' => '',
'SCRIPT_FILENAME' => '/usr/share/nginx/html/srv_var.php',
'SCRIPT_NAME' => '/srv_var.php',
'PATH_INFO' => '',
'REQUEST_URI' => '/srv_var.php',
'DOCUMENT_URI' => '/srv_var.php',
'DOCUMENT_ROOT' => '/usr/share/nginx/html',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'GATEWAY_INTERFACE' => 'CGI/1.1',
'SERVER_SOFTWARE' => 'nginx/1.4.4',
.....
)
I can not figure where the problem is. Any ideas?
回答1:
I stumbled across a solution to this. The $fastcgi_path_info
var works together with $fastcgi_split_path_info
, and needs to be set within the location block. Here's what worked in our environment:
location ~ [^/]\.php(/|$) {
root /var/www/jurism-php;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
}
There is also an example in the Nginx documentation under fastcgi_split_path_info
.
(... which I now see matches more than one post above. Possibly the PATH_INFO line needs to be set after the include statement, to avoid clobbering the value.)
回答2:
My working configuration is as follows:
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)($|/.*);
try_files $fastcgi_script_name =404;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_ignore_client_abort off;
}
回答3:
Try this:
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
http://wiki.nginx.org/PHPFcgiExample
http://trac.nginx.org/nginx/ticket/321
回答4:
Late answer but it might be useful to someone.
I used the variable REQUEST_URI instead of PATH_INFO. Looks like it contains the same value as PATH_INFO is supposed to have.
回答5:
here is what i got. and it works like a charm.
- nginx 1.10.1
- php 5.6.24
https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/
回答6:
For people getting here, reading things..
The problem seems to be that the regular expression (regex) in
location ~* ^/~(.+?)(/.*\.php)$ {
will never match a uri that does not end on .php, so the other regex will never "catch" anything in the last capturing group.
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
So changing that first regex to something like the following will "fix" that.
location ~ [^/]\.php(/|$) {
回答7:
I have come across this issue but my scenario is slightly different as I use try_files
in my directive. Here is my config along with technical explanations:
This is what my location
block looks like
location / {
include php-fpm.conf;
try_files $uri $uri/ /index.php =404;
}
and php-fpm.conf
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+?\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
set $path_info $fastcgi_path_info;
include fastcgi.conf;
fastcgi_param PATH_INFO $path_info;
Two special notices here:
- I installed
nginx
viabrew
and it did not containPATH_INFO
param so I had to add it manually (taken from here)
fastcgi_param PATH_INFO $fastcgi_path_info;
- using
try_files
is a special case (source)
The try_files directive changes URI of a request to the one matched on the file system, and subsequent attempt to split the URI into $fastcgi_script_name and $fastcgi_path_info results in empty path info - as there is no path info in the URI after try_files.
- so what we do is save
INFO_PATH
to a temporary variable and then setINFO_PATH
using that temporary variable
来源:https://stackoverflow.com/questions/20848899/nginx-phpfpm-path-info-always-empty