To enable nginx to support PATH_INFO, you must first know what pathinfo is and why do you need pathinfo? Pathinfo is not a function of nginx, and pathinfo is a function of php. Php has two pathinfo, one is the environment variable $ _ SERVER [& amp; #39; PATH_INFO & amp; #39;]; the other is pa... to enable nginx to support PATH_INFO, you must first know what pathinfo is and why do you need pathinfo?
Pathinfo is not a function of nginx, and pathinfo is a function of php.
There are two pathinfo in php, one is the environment variable $ _ SERVER ['path _ info']; the other is the pathinfo function, and the pathinfo () function returns the file PATH information in the form of an array ;.
Nginx can only set the value of $ _ SERVER ['path _ INFO.
The following is an intuitive example. Let's talk about the functions of the two pathinfo types in php and how to make nginx support pathinfo.
Two pathinfo in php
Pathinfo () in php ()
The pathinfo () function can determine the input path and return the file path information in an array. the array contains the following elements.
[Dirname] Directory of the path
[Basename] file name with suffix
[Extension] file suffix
[Filename] file name without a suffix (php5.2 or a later version is required)
For example
The global variable $ _ SERVER ['path _ info'] in PHP. PATH_INFO is a CGI 1.1 standard and is often used as a parameter carrier.
Many systems are used to optimize the url path format, the most famous such as 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 ', in this case, $ _ SERVER ['query _ string'] = 'C = index & m = search ';
If you don't use the advanced method, is the http://www.test.com/index.php in php? URLs such as type = search are very common. most people may think that they are not very nice and unfriendly to search engines (in fact, whether there is any influence unknown ), because the current search engine is very intelligent and can generate webpage extensions with parameters, we still want to rewrite the URL for the sake of cleanliness,
The following is a simple code for parsing and rewriting with PATH_INFO:
If (! Isset ($ _ SERVER ['path _ info'])
{
$ Pathinfo = 'default ';
}
Else {
$ Pathinfo = explode ('/', $ _ SERVER ['path _ info']);
}
With the above, we can intervene in nginx's support for $ _ SERVER ['path _ info. Before that, we will introduce the configuration parameter cgi. fix_pathinfo in php. ini. it is used to set whether php provides absolute path information or PATH_INFO information in cgi mode. Before using this parameter, PHP sets the absolute path PATH_TRANSLATED to SCRIPT_FILENAME without the PATH_INFO value. Set this parameter to cgi. after fix_pathinfo = 1, cgi sets the complete path information PATH_TRANSLATED to SCRIPT_FILENAME and PATH_INFO. if it is set to cgi. if fix_pathinfo = 0, only the absolute path PATH_TRANSLATED is set to SCRIPT_FILENAME. The default value of cgi. fix_pathinfo is 1.
Nginx does not set the value of the PATH_INFO environment variable by default. you need to use cgi in php. fix_pathinfo = 1 to obtain the path information, but it brings security risks. fix_pathinfo = 0 is set to 0, so that php cannot obtain the PATH_INFO information, and those programs that rely on PATH_INFO for URL beautification will become invalid.
1. you can use the rewrite method to replace PATH_INFO in php.
Example: thinkphp pathinfo solution
Set URL_MODEL = 2
Location /{
If (! -E $ request_filename ){
Rewrite ^/(. *) $/index. php? S =/$1 last;
}
}
2. set The PATH_INFO value in the 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
The old version of nginx is configured as follows:
Location ~ . Php ($ | /){
Set $ script $ uri;
Set $ path_info "";
If ($ uri ~ "^ (. +. Php) (/. + )"){
Set $ script $1;
Set $ path_info $2;
}
The new version of nginx can also use the fastcgi_split_path_info command to set PATH_INFO. the old method is not recommended. add the following configuration in the location section.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service