Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement pathinfo pattern in Nginx

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how Nginx implements the pathinfo pattern. I hope you will get something after reading this article. Let's discuss it together.

What is the pathinfo mode?

Pathinfo is a kind of pseudo-static, we first explain the concept of pseudo-static, pseudo-static page is a bridge between static URL and dynamic URL, it refers to the dynamic URL through URL rewriting means to remove its dynamic parameters, so that URL static, but in the actual web page directory does not rewrite URL. To put it simply, pseudo-static URL is to convert a disguised file name or address through the server to make the page similar to a static page, but there is no independent file on the server, and its essence is a dynamic page.

Anyone who has used the ThinkPHP framework to develop applications should know that it has a URL pattern called pathinfo, which looks similar to the following URL:

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

In fact, the prototype of the URL above looks like the following:

/ / this is the authentic URL, and the one above is the fake http://example.com/index.php?m=module&c=controller&a=action&key1=value1&key2=value2.

Since the URL of pathinfo mode is not authentic, why not use the authentic one? is it not good to use the authentic one? It does have an advantage over the authentic URL,pathinfo model. Here are a few of its benefits.

It provides the best SEO support

Pseudo-static of URL can be realized.

It looks simpler and more beautiful.

URL in pathinfo mode has so many advantages that we must support it. ThinkPHP for more URL mode configuration and implementation, you can refer to this document.

This article mainly discusses the implementation of URL of pathinfo pattern in Nginx, which is not described in the official ThinkPHP documentation, but it is also quite simple to implement. Here are some thoughts on the implementation process.

A standard generic URL format is similar to the following:

: /: @: /;? #

With reference to the general URL format, we can find that there are two big differences between the pathinfo schema and the standard mode URL, one is that there is no index.php file, and the other is that the query parameters do not use the symbol "?" Separate it.

All you need to do now is to restore the pathinfo mode URL request received by Nginx to the standard URL mode so that the server can handle it properly.

Make up the missing index.php file

This requires the rewrite instruction of Nginx, which replaces the requested URI with the target URL. What needs to be achieved here is to set the

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

Replace with

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

Of course, not all URI will rewrite this rule, only those URI that are not files will rewrite. Therefore, the instruction for rewriting will be similar to the following:

# if the requested file does not exist, rewrite the URI # add the entry file index.phpif (!-e $request_filename) {rewrite ^ / (. *) $/ index.php/$1 last;} based on the original

Through the above configuration, you can achieve the effect of completing the index.php entry file.

Distinguishing symbols? The content before and after

In the general URL, the symbol "?" Is used to separate the query string from the previous file. In the URL of pathinfo mode, the symbol "?" No, that is, the server cannot tell which are files and which are query strings in URI. So, our goal is to make the symbol "?" in the pathinfo schema. The contents of the distinction are separated manually.

Fortunately, there is an instruction in Nginx that can achieve our purpose, 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 about fastcgi_split_path_info, please refer to here

The relevant 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;}

A simple example.

Through the configuration of the above two parts, the Nginx server now supports URL in pathinfo mode. The following is a simple server configuration for reference only:

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;}} after reading this article, I believe you have a certain understanding of "how Nginx implements pathinfo pattern". If you want to know more about it, welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report