Nginx Configure pathinfo and thinkphp URL rewrite mode support _nginx

Source: Internet
Author: User
Tags nginx server

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

Copy Code code as follows:

Location ~. *. (PHP|PHP5)? $
{
#原有代码
}

Modify to the following code:

Copy Code code as follows:

#去掉 $ is to match the end of the line, that is, it can be matched. php/to achieve PathInfo
#如果你不需要用到php5后缀, you can also remove it
Location ~. php
{
#原有代码

#定义变量 $path _info, for storing pathinfo information
Set $path _info "";
#定义变量 $real _script_name, for storing real addresses
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, you also need to configure the rewrite rule if you want to support the thinkphp Url_mode set to 2 mode. Locate the Access_log statement, and add the following statement above it:

Copy Code code as follows:

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

Finally, save the configuration file, restart the Nginx service, the thinkphp Url_model set to 2, visit your page, if the normal access, congratulations to your pathinfo configuration succeeded ^_^

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.