How does Nginx implement the method of pathinfo pattern

  • 2020-05-13 04:40:42
  • OfStack

What is pathinfo mode?

pathinfo is a kind of pseudo-static, we first explain the concept of pseudo-static 1, pseudo-static page is a bridge between static URL and dynamic URL, it refers to the dynamic web site by URL rewrite means to remove its dynamic parameters, so that URL static, but in the actual web directory did not rewrite URL. In simple terms, pseudo-static URL is a page that is similar to a static page through server transformation to disguise the file name or address, but there are no independent files on the server, and its essence is still a dynamic page.

Those of you who have used the ThinkPHP framework for developing applications will know that it has one URL mode, pathinfo, which looks like URL:


http://example.com/module/controller/action/key1/value1/key2/value2.html

The prototype of the URL above actually looks like this:


//  This is authentic  URL, The top one is a fake 
http://example.com/index.php?m=module&c=controller&a=action&key1=value1&key2=value2

Since pathinfo URL is not authentic, why not use authentic? Is it not good to use authentic? The pathinfo model does have some advantages over the authentic URL. Here are a few of its benefits.

It provides the best SEO support Pseudo-static implementation of URL is possible It looks simpler and better

URL in pathinfo mode has so many advantages that we must support it. More ThinkPHP configuration and implementation of URL mode can be found in this document.

This article focuses on the implementation of URL in Nginx in pathinfo mode. ThinkPHP is not covered in the official ThinkPHP documentation, but it is quite simple to implement. Here are some thoughts on the implementation process.

A standard common URL format looks like this:


<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>

By referring to the general URL format, it can be found that compared with the standard URL format, there are two big differences between pathinfo mode and URL mode. One is that the index.php file is missing, and the other is that the query parameters are not marked "? ". Separated.

Now all you have to do is when Nginx receives an pathinfo request for URL mode, revert it to standard URL mode so that the server can process it normally.

Fill in the missing index.php file

This requires the rewrite directive of Nginx, which replaces the requested URI with the target URL. What you need to achieve here is that you will


http://example.com/module/controller/action/key1/value1/key2/value2.html

replace


http://example.com/index.php/module/controller/action/key1/value1/key2/value2.html

Of course, not all URI will be overwritten for this rule, only those URI that are not files will be overwritten. So, the instructions for overwriting will look like this:


#  If the requested file does not exist, proceed  URI  rewrite 
#  Add the entry file to the original  index.php
if (!-e $request_filename) {
 rewrite ^/(.*)$ /index.php/$1 last;
}

With the above configuration, you can complete the index.php entry file.

Distinguish symbols? Before and after

In common URL, the symbol "?" Is used to separate the query string from the previous file. In pathinfo mode, URL, the symbol "?" No, that is, the server cannot tell which files are in URI from which query strings are in URI. So, our goal is to turn pathinfo mode into a mode that should be composed of symbols "?" The separated content is separated manually.

Fortunately, there is a directive in Nginx that does what we want, fastcgi_split_path_info. It assigns the two strings defined by the regular expression to the variable $fastcgi_script_name and the variable $fastcgi_path_info, respectively, for later use. For more information on fastcgi_split_path_info, see here

The configuration is similar to the following code:


location ~ ^(.+\.php)(.*)$ {
 root /var/www/html/$vhost_path;
 fastcgi_pass phpfpm:9000;
 fastcgi_split_path_info ^(.+\.php)(.*)$;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_param PATH_INFO $fastcgi_path_info;

 include fastcgi_params;
}

One simple example

Through the above two-part configuration, Nginx server now supports URL in pathinfo mode. The following is a simple server configuration for your reference:


server {
 listen 80;
 server_name tp5.loc;

 set $vhost_path tp5/public;

 location / {
 root /usr/share/nginx/html/$vhost_path;
 index index.php index.html index.htm;

 if (!-e $request_filename) {
  rewrite ^/(.*)$ /index.php/$1 last;
 }
 }

 location ~ ^(.+\.php)(.*)$ {
 root /var/www/html/$vhost_path;
 fastcgi_pass phpfpm:9000;
 fastcgi_split_path_info ^(.+\.php)(.*)$;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_param PATH_INFO $fastcgi_path_info;

 include fastcgi_params;
 }
}

conclusion


Related articles: