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 support PATH_INFO under nginx

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to support PATH_INFO under nginx, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

For nginx to support PATH_INFO, you first need to know what pathinfo is and why use pathinfo?

Pathinfo is not a function of nginx, pathinfo is a function of php.

There are two pathinfo in php, one is the environment variable $_ SERVER ['PATH_INFO'], and the other is the pathinfo function, which returns the information of the file path as an array.

All nginx can do is set the $_ SERVER ['PATH_INFO] value.

The following examples are more intuitive. First talk about the role of two kinds of pathinfo in php, and then talk about how to make nginx support pathinfo.

Two pathinfo in php

Pathinfo () in php

The pathinfo () function determines the input path and returns the information of the file path in the form of an array containing the following elements.

Directory of the [dirname] path

[basename] File name with suffix

[extension] File suffix

[filename] No suffix file name (php5.2 or above is required)

For example

Output

Array

(

[dirname] = > / nginx

[basename] = > test.txt

[extension] = > txt

[filename] = > test

)

$_ SERVER in php ['PATH_INFO']

The global variable $_ SERVER ['PATH_INFO'] in PHP, PATH_INFO is a CGI 1.1 standard and is often used as a parameter carrier.

It is 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 point $_ SERVER ['QUERY_STRING'] =' centering indexation masked search'

If you do not use advanced methods, URL such as http://www.test.com/index.php?type=search in php is very common, and most people may not feel very beautiful and very unfriendly to search engines (in fact, it is unknown whether there is any impact), because today's search engines are already smart enough to include suffix pages with parameters, but people still want to rewrite URL for cleanliness.

Here is a very simple piece of code that parses and rewrites with PATH_INFO:

With that in mind, we can get involved in nginx's support for $_ SERVER ['PATH_INFO']. Before that, we will 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 cgi mode. Before this parameter, PHP sets the absolute path PATH_TRANSLATED to script _ FILENAME, and there is no PATH_ info value. When this parameter is set to cgi.fix_pathinfo=1, cgi sets the value of complete path information PATH_TRANSLATED to script _ FILENAME, and sets PATH_INFO information; if set to cgi.fix_pathinfo=0, only the value of absolute path PATH_TRANSLATED is set to script _ FILENAME. The default value for cgi.fix_pathinfo is 1.

By default, nginx does not set the value of PATH_INFO environment variable, and requires php to use cgi.fix_pathinfo=1 to obtain path information, but it also brings security risks. You need to set cgi.fix_pathinfo=0 to 0, so that php cannot get PATH_INFO information, and programs that rely on PATH_INFO for URL beautification are invalid.

1. PATH_INFO in php can be replaced by rewrite

Example: thinkphp's pathinfo solution

Set up URL_MODEL=2

Location / {

If (!-e $request_filename) {

Rewrite ^ / (. *) $/ index.php?s=/$1 last

}

}

Set PATH_ info value in 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 as follows

Location ~ .php ($| /) {

Set $script $uri

Set $path_info ""

If ($uri ~ "^ (. + .php) (/. +)") {

Set $script $1

Set $path_info $2

}

Fastcgi_pass 127.0.0.1:9000

Fastcgi_index index.php

Fastcgi_param SCRIPT_FILENAME $document_root$script

Fastcgi_param SCRIPT_NAME $script

Fastcgi_param PATH_INFO $path_info

}

The new version of nginx can also use the fastcgi_split_path_info directive to set PATH_INFO. The old method is no longer recommended. Add the following configuration in the location section.

Location ~ ^. +\ .php {

(.)

Fastcgi_split_path_info ^ ((? U). +\ .php) (/. +) $

Fastcgi_param SCRIPT_FILENAME / path/to/php$fastcgi_script_name

Fastcgi_param PATH_INFO $fastcgi_path_info

Fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info

(.)

}

Finally, someone may ask why this problem does not occur in apache.

Apache generally runs php,apache as a module. The value of $_ SERVER ['PATH_INFO'] can be set without additional configuration.

Thank you for reading this article carefully. I hope the article "how to support PATH_INFO under nginx" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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