PathInfo and URL rewrite mode with thinkphp supported under Nginx

Source: Internet
Author: User
Tags fpm nginx server

here's how to make Nginx support thinkphp pathinfo and URL rewrite mode. 1, Thinkphp gives the official solution of thinkphp, as follows:

Open Nginx configuration file/etc/nginx/nginx.cof is generally in this path, depending on your installation path may vary. If you configure Vhost, and you only need this one vhost support PathInfo, you can open your Vhost configuration file directly. Find code similar to the following (different versions of Nginx may be slightly different, but not very far from the difference):

    Location ~. php        {                #原有代码                                #定义变量 $path _info for storing pathinfo information                set $path _info "";                #定义变量 $real _script_name for storing the real address                set $real _script_name $fastcgi _script_name;                #如果地址与引号内的正则表达式匹配                if ($fastcgi _script_name ~ "^ (. +?\.php) (/.+) $") {                        #将文件地址赋值给变量 $real _script_name                        set $ Real_script_name $;                        #将文件地址后的参数赋值给变量 $path _info                        set $path _info;                }                #配置fastcgi的一些参数                fastcgi_param script_filename $document _root$real_script_name;                Fastcgi_param script_name $real _script_name;                Fastcgi_param path_info $path _info;        }

In this way, the Nginx server can support PathInfo. However, if you want to support thinkphp's Url_mode setting to 2, you also need to configure the rewrite rule. Locate the Access_log statement, and add the following statement above it:

    #如果请求既不是一个文件, nor is it a directory, then execute the rewrite rule    if (!-e $request _filename)    {            #地址作为将参数rewrite到index. php.            rewrite ^/(. *) $/index.php/$1;            #若是子目录则使用下面这句, change the subdir to a directory name.            #rewrite ^/subdir/(. *) $/subdir/index.php/$1;    }

Although the above method is thinkphp official given, presumably is also verified, but the sad urge is to me does not work.

2, my solution

I was configured under sites (Vhost) under the/etc/nginx/sites-available/directory. Of course you can also configure directly in the/etc/nginx/nginx.conf.
Add the following code in the localhost/{} configuration:

#如果请求既不是一个文件, nor is it a directory, then execute the rewrite rule    if (!-e $request _filename)    {            #地址作为将参数rewrite到index. php.            rewrite ^/(. *) $/index.php/$1;            #若是子目录则使用下面这句, change the subdir to a directory name.            #rewrite ^/subdir/(. *) $/subdir/index.php/$1;    }

The complete code is as follows:

Location/{                root/var/www;                # First attempt to serve request as file, then                # as directory, then fall back to index.html                try_files $uri $uri// index.html;                # Uncomment to enable Naxsi in this location                # include/etc/nginx/naxsi.rules                if (!-e $request _filename)                {
   rewrite ^/phpparser/(. *) $/phpparser/index.php?s=$1 last;                        break;                }        }

Then add the following two lines in the localhost ~ \.php{} configuration column:

Fastcgi_split_path_info ^ (. +\.php) (. *) $;                             

The complete configuration is as follows

Location ~ \.php$ {                root/var/www;                Try_files $uri = 404;                Fastcgi_split_path_info ^ (. +\.php) (/.+) $;        #       # note:you should has "cgi.fix_pathinfo = 0;" In php.ini        #        # # with       php5-cgi alone:                #fastcgi_pass 127.0.0.1:9000;                Fastcgi_split_path_info ^ (. +\.php) (. *) $;                Fastcgi_param path_info $fastcgi _path_info;        # with       php5-fpm:                fastcgi_pass unix:/var/run/php5-fpm.sock;                Fastcgi_index index.php;                Include Fastcgi_params;        }

PathInfo and URL rewrite mode with thinkphp supported under Nginx

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.