The path_info Problem of Nginx (php/fastcgi)

Source: Internet
Author: User

Transferred from: http://www.laruence.com/2009/11/13/1138.html

Path_info is a standard for CGI 1.1 and is often used as a reference vector.

For example, we can use path_info instead of rewrite to implement pseudo-static pages, and many PHP frameworks use Path_info as routing vectors.

In Apache, when not configured, Acceptpathinfo is accepted by default for PHP scripts, which means:

If there is a/laruence/index.php in the server

So, for the following request,

    1. /laruence/index.php/dummy
    2. /laruence/dummy

Apache accepts that it will be considered a visit to info.php and will set Path_info to dummy

For Nginx, path INFO is not supported, that is, it does not set path_info by default.

Because the default configuration file support for PHP is only very basic, so for the default configuration for the above access will be 404, the prompt to find a file error.

This is fatal for some PHP frameworks that use PATH_INFO to deliver critical information (such as Kohana, thinkphp).

For this problem, there are generally two solutions, the first is the use of rewrite, but the shortcomings of this method is also very obvious, need to convert Path_info to query String. This is not the way to explain it ~

And, the second approach is what I'm going to mention today, analog path_info:

First, we know that in Nginx, it is by matching the filename extension, to decide whether to give the PHP CGI server to explain. The following default configuration sections are generally available in nginx.conf:

    1. Location ~. php$ {
    2. Fastcgi_index index.php;
    3. Fastcgi_pass 127.0.0.1:9000;
    4. Include Fastcgi_params;
    5. }

Therefore, for a file path such as/laruence/info.php/pathinfo, Nginx will not be properly handed to the PHP CGI server. So we need to rewrite this configuration to:

    1. Location ~ . php {//fragment match
    2. Fastcgi_index index. php;
    3. Fastcgi_pass 127.0.0.1:9000;
    4. Include Fastcgi_params;
    5. }

The script path is now being processed by PHP itself. So how do you add path_info?

First, we need to open the PHP cgi.fix_pathinfo configuration item, after opening this configuration item, PHP will go to the CGI specification to check that part of the Script_filename is Access script and path_info (INI configuration explanation), And according to Script_name to modify the Path_info (and path_translated) as the correct value (in fact, that is, PHP initial support for CGI 1.1 is not in place)

Then, just add a Fastcgi_param entry:

    1. Location ~. php {
    2. Fastcgi_index index.php;
    3. Fastcgi_pass 127.0.0.1:9000;
    4. Include Fastcgi_params;
    5. Fastcgi_param path_info $fastcgi _script_name;
    6. }

Now try it ...

BTW: Of course, the above solution, the analysis of the path to the PHP to deal with, the network also has a friend to give another method of configuration, this method is the Nginx to analyze the path (also do not need Fix_pathinfo):

    1. Location ~ \.php
    2. {
    3. Fastcgi_index index.php;
    4. Fastcgi_pass 127.0.0.1:9000;
    5. Include Fastcgi_params;
    6. Set $path _info "";
    7. Set $real _script_name $fastcgi _script_name;
    8. if ($fastcgi _script_name ~ "^ (. +?\.php) (/.+) $") {
    9. Set $real _script_name $;
    10. Set $path _info;
    11. }
    12. Fastcgi_param script_filename/var/html/$real _script_name;
    13. Fastcgi_param script_name $real _script_name;
    14. Fastcgi_param path_info $path _info;
    15. }

The path_info Problem of Nginx (php/fastcgi)

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.