Nginx Support Path_info method examples in detail _nginx

Source: Internet
Author: User
Tags nginx server

The method of supporting Path_info under Nginx is analyzed in this paper. Share to everyone for your reference, specific as follows:

To let Nginx support Path_info, first need to know what is pathinfo, why to use PathInfo?

PathInfo is not a nginx function, PathInfo is the function of PHP.

There are two pathinfo in PHP, one is the environment variable $_server[' Path_info ', the other is the PathInfo function, and the PathInfo () function returns the file path information as an array;.

What Nginx can do is set the $_server[' Path_info ' value.

Below we illustrate the more intuitive. First say the role of two pathinfo in PHP, and how to let Nginx support PathInfo.

Two pathinfo in PHP

PathInfo in PHP ()

The PathInfo () function can judge the path entered and return the information of the file path as an array containing the following elements.

directory for [dirname] Paths
[basename] with suffix file name
[Extension] file suffix
[filename] without suffix file name (need php5.2 above version)

For example

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

Output

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

$_server[' Path_info ' in PHP

The global variable $_server[' path_info '],path_info in PHP is a standard for CGI 1.1, which is often used as a reference carrier.

Used by many systems to optimize URL path formats, 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 time $_server[' query_string ' = ' c=index&m=search ';

Without advanced methods, URLs such as Http://www.test.com/index.php?type=search in PHP are common, and most people may find it unattractive and very unfriendly to search engines (in fact, there is no effect unknown), Because now the search engine is very intelligent, you can pay the suffix page with parameters, but for the sake of neat consideration or want to be able to rewrite the URL,

Here's a very simple piece of code that parses the rewrite 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 understanding we can intervene in Nginx's support for $_server[' Path_info '. Before that, we'll introduce the configuration parameter Cgi.fix_pathinfo in a php.ini, which is used to provide absolute path information or path_info information for PHP in the settings CGI mode. The value of PHP setting absolute path path_translated before this parameter is script_filename and there is no path_info value. After setting this parameter to Cgi.fix_pathinfo=1, CGI sets the full path information path_translated value to Script_filename and sets the PATH_INFO information; if set to cgi.fix_pathinfo= 0 Set the absolute path path_translated value to Script_filename only. The default value for Cgi.fix_pathinfo is 1.

Nginx default is not set the value of PATH_INFO environment variables, PHP needs to use Cgi.fix_pathinfo=1 to complete the path information acquisition, but at the same time will bring security risks, need to set the cgi.fix_pathinfo=0 to 0, In this way, PHP can not get path_info information, those who rely on Path_info URL beautification program is invalid.

1. Can replace the path_info in PHP by rewrite way

Example: Thinkphp's PathInfo solution

Set url_model=2

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

Setting the Path_info value in the 2.nginx configuration file

The requested URL is/abc/index.php/abc

The value of the PATH_INFO is/ABC
The value of the Script_filename is $doucment_root/abc/index.php
script_name/abc/index.php

Older versions of the Nginx are configured using the following methods

Location ~. PHP ($|/) {
  set $script $uri;
  Set $path _info "";
  if ($uri ~ "^ (. +.php) (/.+)") {
    set $script $;
    Set $path _info $
  }
  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 way is no longer recommended, and the following configuration is added to the location segment.

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 does Apache not have this problem?

Apache is typically run as a module Php,apache can be set on the value of $_server[' path_info ' and no additional configuration is required.

This article permanent address:http://blog.it985.com/7768.html
This article comes from IT985 blog, reprint, please indicate the source and corresponding link.

I hope this article will help you with your Nginx server configuration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.