nginx support PATH_INFO method example details

  • 2020-05-10 23:29:23
  • OfStack

This article gives an example of a method that supports PATH_INFO under nginx. I will share it with you for your reference as follows:

In order for nginx to support PATH_INFO, you first need to know what pathinfo is and why 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']; The other is the pathinfo function. The pathinfo() function returns the information of the file path in the form of an array. .

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

Let's give you an example that's intuitive. Let's talk about the two pathinfo functions in php, and how to get nginx to support pathinfo.

Two of pathinfo in php

The pathinfo php ()

The pathinfo() function determines the input path and returns information about the file path as an array containing the following elements.

[dirname] directory of the   path
[basename] file name with suffix
[extension]   file suffix
[filename]   without suffixed file name (php5.2 or above)

For example,


<?php
print_r(pathinfo("/nginx/test.txt"));
?>

The output


Array
(
  [dirname] => /nginx
  [basename] => test.txt
  [extension] => txt
  [filename] => test
)

The $_SERVER php [' PATH_INFO]

The global variable $_SERVER['PATH_INFO'] in PHP, PATH_INFO is a standard of CGI 1.1 and is often used as a reference carrier.

It is used by many systems to optimize the url path format, most notably the THINKPHP framework.

For the following url:

http://www.test.cn/index.php/test/my.html?c=index&m=search

We get $_SERVER['PATH_INFO'] = '/test/ my.html ', while $_SERVER['QUERY_STRING'] = 'c=index&m=search';

Without advanced method, php http: / / www test. com/index php & # 63; Like = search type URL are common, most people don't think beauty but also for search engines is also very friendly is it (in fact unknown), because now already very intelligent search engine, can income with parameters of suffix web page, but all for the sake of neat still want to want to be able to rewrite URL,

The following is a very simple piece of code that is rewritten using PATH_INFO:


<?php
if(!isset($_SERVER['PATH_INFO'])) {
  $pathinfo = 'default';
} else {
  $pathinfo = explode('/', $_SERVER['PATH_INFO']);
}
if(is_array($pathinfo) && !empty($pathinfo)) {
  $page = $pathinfo[1];
} else {
  $page = 'default.php';
}
?>

With this in mind, we can intervene in the issue of nginx support for $_SERVER['PATH_INFO']. This is preceded by an php.ini configuration parameter, cgi.fix_pathinfo, which is used to provide absolute path information or PATH_INFO information for php in cgi mode. Without this parameter, PHP sets the absolute path PATH_TRANSLATED to SCRIPT_FILENAME, without PATH_INFO. After setting this parameter to cgi.fix_pathinfo =1, cgi sets the full path information PATH_TRANSLATED to SCRIPT_FILENAME, and PATH_INFO to SCRIPT_FILENAME; If set to cgi.fix_pathinfo =0, then only the absolute path PATH_TRANSLATED is set to SCRIPT_FILENAME. The default value for cgi.fix_pathinfo is 1.

By default, nginx will not set the value of PATH_INFO environment variable, php needs to use cgi.fix_pathinfo =1 to complete the path information acquisition, but it will bring security risks at the same time, cgi.fix_pathinfo =0 to set php to 0, so php will not get PATH_INFO information, and those relying on PATH_INFO for URL beautification will fail.

1. PATH_INFO in php can be replaced by rewrite

Example: thinkphp's pathinfo solution

Set URL_MODEL = 2


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

2. The PATH_INFO value is set in the nginx configuration file

The requested url/abc/index php/abc

The value of PATH_INFO is /abc
SCRIPT_FILENAME value is $doucment_root/abc/index php
SCRIPT_NAME /abc/index.php

The older version of nginx was 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, one might ask why apache doesn't have this problem.

apache1 normally runs php as a module, and apache can be set to the value of $_SERVER['PATH_INFO'] without additional configuration.

In this paper, the permanent address: http: / / blog it985. com / 7768. html
This article is from IT985 blog, please indicate the source and the corresponding link when reprinted.

I hope this article has helped you with your nginx server configuration.


Related articles: