Ubuntu下Nginx配置ThinkPHP的Pathinfo和URl Rewrite模式
概述
在上一篇文章Nginx配置Thinkphp支持URLRewrite中已经介绍了如何配置Nginx支持ThinkPHP的URLRewrite,但是上文针对的是Centos平台,这次因为某些特殊的原因,服务器环境必须用ubuntu,本来以为和Cetons中一模一样,但是配置完了发现不能使用,所以就百度了一些文章。
配置方法
TP官方解决方案
location~.php
{
#原有代码
#定义变量$path_info,用于存放pathinfo信息
set$path_info"";
#定义变量$real_script_name,用于存放真实地址
set$real_script_name$fastcgi_script_name;
#如果地址与引号内的正则表达式匹配
if($fastcgi_script_name~"^(.+?\.php)(/.+)$"){
#将文件地址赋值给变量$real_script_name
set$real_script_name$1;
#将文件地址后的参数赋值给变量$path_info
set$path_info$2;
}
#配置fastcgi的一些参数
fastcgi_paramSCRIPT_FILENAME$document_root$real_script_name;
fastcgi_paramSCRIPT_NAME$real_script_name;
fastcgi_paramPATH_INFO$path_info;
}
这样,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;
}
网友解决方案
location/{
root/var/www;
#Firstattempttoserverequestasfile,then
#asdirectory,thenfallbacktoindex.html
try_files$uri$uri//index.html;
#Uncommenttoenablenaxsionthislocation
#include/etc/nginx/naxsi.rules
if(!-e$request_filename)
{
rewrite^/PHPParser/(.*)$/PHPParser/index.php?s=$1last;
break;
}
}
然后在localhost~.php{}配置栏目中添加如下两行:
fastcgi_split_path_info^(.+\.php)(.*)$; fastcgi_paramPATH_INFO$fastcgi_path_info;
完整配置如下:
location~\.php${
root/var/www;
try_files$uri=404;
fastcgi_split_path_info^(.+\.php)(/.+)$;
# #NOTE:Youshouldhave"cgi.fix_pathinfo=0;"inphp.ini
#
# #Withphp5-cgialone:
#fastcgi_pass127.0.0.1:9000;
fastcgi_split_path_info^(.+\.php)(.*)$;
fastcgi_paramPATH_INFO$fastcgi_path_info;
# #Withphp5-fpm:
fastcgi_passunix:/var/run/php5-fpm.sock;
fastcgi_indexindex.php;
includefastcgi_params;
}