To let Nginx support Path_info, first need to know what is pathinfo, why use PathInfo?
PathInfo is not the function of Nginx, PathInfo is the function of PHP.
There are two pathinfo in PHP, one is environment variable $_server[' path_info '; the other is the PathInfo function, and the PathInfo () function returns the file path information as an array.
The only thing Nginx can do is set the value of $_server[' path_info].
Below we illustrate the more intuitive. First of all, the role of two pathinfo PHP, and how to let Nginx support PathInfo.
Two pathinfo in PHP
PathInfo () in PHP
The PathInfo () function can determine the input path, returning the information in the form of an array to the file path, and the array contains the following elements.
- directory for [dirname] Paths
- [basename] with suffix file name
- [Extension] file suffix
- [FileName] does not have a suffix file name (more than php5.2 version required)
For example
Print_r (PathInfo ("/nginx/test.txt")); output: Array ( [dirname] =/nginx [basename] = Test.txt [ Extension] = txt [filename] = test)
$_server[' Path_info ' in PHP
Global variables in PHP $_server[' path_info '],path_info is a standard for CGI 1.1 and is often used as a reference vector.
Used by many systems to optimize the URL path format, the most famous such as the thinkphp framework.
For the following URL:
Http://www.test.cn/index.php/test/my.html?c=index&m=search
We can get $_server[' path_info '] = '/test/my.html ', and at this time $_server[' query_string '] = ' c=index&m=search ';
Without the use of advanced methods, URLs like Http://www.test.com/index.php?type=search in PHP are common, and most people may find it unattractive and very unfriendly to search engines (in fact, it doesn't affect the unknown). Because now the search engine has been very smart, you can get a suffix page with parameters, but for the sake of neatness or want to be able to rewrite the URL,
The following is a very simple code that parses a rewrite using path_info:
if (!isset ($_server[' path_info ')) { $pathinfo = ' default ';} else{ $pathinfo = explode ('/', $_server[' path_info ');} if (Is_array ($pathinfo) &&!empty ($pathinfo)) { $page = $pathinfo [1];} else{ $page = ' default.php ';}
With this knowledge we can intervene in Nginx's support for $_server[' Path_info '. Before this, we also introduce a configuration parameter Cgi.fix_pathinfo in PHP.ini, which is used to provide absolute path information or path_info information for PHP in the set CGI mode. Without this parameter, PHP sets the absolute path path_translated value to Script_filename, without the Path_info value. After setting this parameter to Cgi.fix_pathinfo=1, the CGI sets the full path information path_translated the value to Script_filename and sets the PATH_INFO information, if set to cgi.fix_pathinfo= 0 only the absolute path path_translated is set to the value of Script_filename. The default value for Cgi.fix_pathinfo is 1.
Nginx default is not set the value of the PATH_INFO environment variable, PHP needs to use Cgi.fix_pathinfo=1 to complete the path information acquisition, but also a security risk, you need to set the cgi.fix_pathinfo=0 to 0, In this way, PHP will not get path_info information, and those who rely on Path_info for URL beautification program will be invalidated.
1. You can replace the path_info in PHP by rewrite
Example: PathInfo solution for thinkphp
Set url_model=2
Location/{ if ' (!-e $request _filename) { rewrite ^/(. *) $/index.php?s=/$1 last; }}
Setting the Path_info value in a 2.nginx configuration file
The requested URL is/abc/index.php/abc
The value of Path_info is/ABC
The value of Script_filename is $doucment_root/abc/index.php
script_name/abc/index.php
Older versions of Nginx are configured using the following method
Location ~ \.php { #fastcgi_pass unix:/tmp/php-cgi.sock; Fastcgi_pass 127.0.0.1:9000; Fastcgi_index index.php; Include Fastcgi_params; Set $real _script_name $fastcgi _script_name; if ($fastcgi _script_name ~ "^ (. +?\.php) (/.+) $") { set $real _script_name $; Set $path _info; } Fastcgi_param script_filename $document _root$real_script_name; Fastcgi_param script_name $real _script_name; Fastcgi_param path_info $path _info; }
After finishing the nginx.conf code:
Location/{ if ' (!-e $request _filename) { rewrite ^/(. *) $ /index.php/$1 last ; break; } } Location ~ \.php { #fastcgi_pass unix:/tmp/php-cgi.sock; Fastcgi_pass 127.0.0.1:9000; Fastcgi_index index.php; Include Fastcgi_params; Set $real _script_name $fastcgi _script_name; if ($fastcgi _script_name ~ "^ (. +?\.php) (/.+) $") { set $real _script_name $; Set $path _info; } Fastcgi_param script_filename $document _root$real_script_name; Fastcgi_param script_name $real _script_name; Fastcgi_param path_info $path _info; }
Finally summed up: "Note: location ~ \.php {There is no $, originally is location ~ \.php$ {, so remember this also to change to"
Just modify the Nginx inside the configuration file on the line:
Location/{root/usr/share/nginx/html/tp5/public; Index index.php index.htm; if (!-e $request _filename) {rewrite ^/(. *) $/index.php/$1 last; Break }}
Location ~ \.php {
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Fastcgi_param script_filename $document _root$fastcgi_script_name;
Include Fastcgi_params;
Set $real _script_name $fastcgi _script_name;
if ($fastcgi _script_name ~ "^ (. +?\.php) (/.+) $") {
Set $real _script_name $;
Set $path _info;
}
Fastcgi_param script_filename $document _root$real_script_name;
# Fastcgi_param Script_name $real _script_name;
Fastcgi_param path_info $path _info;
}
Nginx configuration PathInfo Mode example