the solution of PATH_INFO on nginx
给机器用上了大名鼎鼎的nginx,据说是高效的web服务器和反向代理服务器,使用一段时间之后觉得的确是名不虚传啊,感觉比较强劲,而且配置比 较灵活,但目前很多php程序多数都是以apache为默认web服务器来编写的,所以一些不成文的规则比较多,所以一些在apache下习惯了的东西突 然弄到nginx下有些不适应,比如rewrite,很多php程序会带一个.htaccess文件来定义rewrite规则,而nginx不接受这个, 那就需要重新。
我的机器安装了Debian lenny RC1,Nginx 0.7.26、php5.2.8、Mysql5.1.30,然而在调试typolightCMS 和 Expressengine 的时候,URI 出现 类似 /typolight/index.php/aboutme.html 或 /expressengine/index.php/site/about/ 的时候,都会出现404错误,网上也查找了一些资料,说是PATH_INFO的问题,我也看了不少解决办法,也一一尝试,但至今没有解决,后来请教了志伟老兄,他给出了解决方案。记录如下:
if (!-e $request_filename) {
rewrite ^/ee/index.php(.*)$ /ee/index.php?$1 last;
}
将/index.php/a/b/c的URL变成 /index.php?/a/b/c
如果写成
if (!-e $request_filename) {
rewrite (.+\.php)(.*)$ $1?$2 last;
}
或者
if (!-e $request_filename) {
rewrite (.+php)(.*)$ $1?$2 last;
}
或者
if (!-e $request_filename) {
rewrite (.+.php)(.*)$ $1?$2 last;
}
就可通用于任何目录,不必限制于 /ee/ 目录
如果服务器支持PATH_INFO环境变量,就没有这么麻烦了,比如
/test/here.html/more 和 /test/nothere.html/more请求的PATH_INFO环境变量都是 /more
在Apache上,如果AcceptPathInfo设置为On, 那么/test/here.html/more是可以接受的,否则返回404错误,因为在一个真实的文件名 here.html
所以,另外一个解决方法是把PATH_INFO传递给PHP
修改
location ~ \.php { # 尤其注意这里,不是 location ~ \.php$,
# 因为$表示结束,这种格式不能匹配index.php/aa/bb这种URL
fastcgi_param PATH_INFO $fastcgi_script_name;

当前暂无评论 »