说明:
Nginx目录:/usr/local/nginx/
Nginx配置文件:/usr/local/nginx/nginx.conf
如果站点使用了vhost虚拟主机,并且只需要这一个虚拟主机支持pathinfo的,可以直接打开你的vhost的配置文件进行设置。
找到类似如下代码:
location ~ .*\.(php|php5)?$
{
#原来的代码
......
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
......
}
修改为以下代码:
location ~ \.php
{
#定义变量 $path_info ,用于存放pathinfo信息
set $path_info "";
#定义变量 $real_
set $real_
#如果地址与引号内的正则表达式匹配
if ($fastcgi_
#将文件地址赋值给变量 $real_
set $real_
#将文件地址后的参数赋值给变量 $path_info
set $path_info $2;
}
#配置fastcgi的一些参数
fastcgi_param
fastcgi_param
fastcgi_param PATH_INFO $path_info;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
这样设置后,nginx已经可以支持pathinfo了。如果要支持ThinkPHP的URL_MODE设置为2的模式,还需要配置rewrite规则。找到access_log语句,在其上方加上以下语句:
#如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
if (!-e $request_filename)
{
#地址作为将参数rewrite到index.php上。
rewrite ^/(.*)$ /index.php/$1;
#若是子目录则使用下面这句,将subdir改成目录名称即可。
#rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
}
最后,重启Nginx服务,service nginx restart 使配置生效。
至此,Nginx下配置支持ThinkPHP的pathinfo模式教程完成。