Provides various official and user-released code examples. For code reference, you are welcome to learn how to rewrite the ThinkPHP-based website url in nginx.
For url rewriting of thinkphp-based applications in nginx, you need to understand the processing logic of various url format parameters in thinkphp and the principle of nginx rewriting. To put it simply, nginx needs to execute a single app portal file under ThinkPHP regardless of the url format, and then parameterize various url formats according to certain format rules, finally, you can run the specified module and controller. In this process, you cannot see the URLs of various rules. The url rules are as follows:
# First common get multi-parameter method
Htt: // www.jb100.net/index.php? M = Info & a = usrInfo
# Pathinfo method 2
Htt: // www.jb100.net/index.php/Info/usrInfo
# Other variables with get
Htt: // www.jb100.net/index.php/Info/usrInfo/Id/1# The third Common get single-parameter method ThinkPHP is also called "compatibility mode"
Htt: // www.jb100.net/index.php? S = Info/usrInfo
# Other variables with get
Htt: // www.jb100.net/index.php? S = Info/usrInfo/Id/1
/*
# Of course, these three modes can also contain file suffixes
# Actually, three types of data are merged.
# The suffix is only a slight change to the above three rules
# After the core code of the TinkPHP framework obtains the parameters of the above three modes
# Any suffix with or without the suffix can be configured for specific processing.
# The following rules
*/
# First
Htt: // www.jb100.net/index.php/Info/usrInfo.html
# Type 2
Htt: // www.jb100.net/index.php? S = Info/usrInfo.html======
What is pathinfo? Pathinfo is a function of php, and nginx does not have the pathinfo function. Write the following code in php:
print_r(pathinfo('http://www.jb100.net/test/index.txt'));
?>Result:
Array
(
[dirname] => http://www.jb100.net/test
[basename] => index.txt
[extension] => txt
[filename] => index
)Through the above running results, we can see that the printed array key field has clearly indicated its meaning.
ThinkPHP uses this idea to format the parameters transmitted from a single url. A typical ThinKPHP-based url example is as follows:
http://www.jb100.net/index.php/Usr/UsrInfoSimply put, the url format formatted above is the pathinfo path mode that ThinkPHP needs to support. This formatted url is explained a little. Visit this address. For websites based on the ThinkPHP framework, what needs to be executed is the index under the root directory of the website. PHP file, and the/Usr/UsrInfo following the url is used as the function module parameter and controller parameter in the mvc idea. If the parameter is converted to the common get method, the above url will be changed:
http://www.jb100.net/index.php?m=Usr&a=UsrInfoOf course, the get parameter variable is m and a in this example. The variable name can be changed in ThinkPHP, this should be based on the specific ThinkPHP-based application module planning and preferences.
With this idea, nginx url rewriting has the most primitive writing method. For the formatted url of pathinfo above, nginx will regard it as a path for execution without url rewriting, generally, no one has to create an index under the root directory of the website. php Directory (Note: it is a directory, not a file in the general sense), so the first type of url rewriting appears:
# The following is the code snippet of the nginx configuration file that only writes the url rewriting part.
location / {
root /var/www;
index index.html index.htm index.php;
if (!-d $request_filename) {
rewrite ^/(.*)/(.*)/*$ /index.php?m=$1&a=$2 last;
break;
}
}However, the problem arises. ThinkPHP-based applications not only use the pathinfo mode in the preceding example, but have now become like this: (for convenience of illustration, the following code segment is called "url example ", the "url example" mentioned above refers to the three url formats)
# First, No. html Suffix in index. php but url.pdf is hidden
http://www.jb100.net/index.php/Usr/UsrInfo.html# Second, hide the index. php script file
Http://www.jb100.net/Usr/UsrInfo
Or
Http://www.jb100.net/Usr/UsrInfo/The third method is to hide index.php and the suffix ".html" in the visitor's website.
http://www.jb100.net/Usr/UsrInfo.htmlNext, let's take a look at the various logic of processing URLs in ThinkPHP. ThinkPHP makes multiple judgments and processes on passing parameters. Besides passing different get variables as control variables in mvc in get mode, there is also a "compatible mode" parameter passing rule, that is, the following:
# Input parameters that ThinkPHP can process
http://www.jb100.net/index.php?s=/Usr/UsrInfoThe parameter passing method is still the get method, but multiple get parameters are omitted. The ThinkPHP Framework processes the get variable $ _ GET ['s '] and extracts the control variables required by mvc.
So another nginx url rewrite occurs:
location / {
root /var/www;
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}Now we need to be compatible with the three formats in the preceding url example. The final solution is as follows:
location / {
root /var/www;
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}Reproduced original address: http://www.jb100.net/html/content-28-519-1.html
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB