首先说明,这个rewrite规则可能纯属个案,是根据程序员需要写的。
Yii
How to hide index.php on nginx?
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
access_log off;
}
Second, we need the following rewrite rule to hide index.php:
location /yiiGuestbook {
if (!-e $request_filename){
rewrite (.*) /yiiGuestbook/index.php/$1;
}
}
上面的配置,定义了一个PATH_INFO $fastcgi_script_name;把对非文件的请求转到/yiiGuestbook/index.php
用上面的解决办法并不符合我们程序员的需要。我们需要的是对http://127.0.0.1/index.php/test/test这样的请求,PATH_INFO等于/test/test,并且把/test/test转到index.php。但是nginx的PATH_INFO变量默认是没有的,只能自己定义。于是有了下面的解决办法。
#add for yiiframework
if (!-e $request_filename){
rewrite (.*) /index.php break;
}
#end for yiiframework
location ~ .*\.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
include fcgi.conf;
fastcgi_pass 127.0.0.1:10080;
fastcgi_index index.php;
set $path_info "";
if ($request_uri ~ "^(.+?\.php)(/.+)$") {
set $path_info $2;
}
if ($request_uri ~ "^(.+?\.php)(.*)(\?.*)$") {
set $path_info $2;
}
fastcgi_param PATH_INFO $path_info;
}
为了代码美观,最后决定用直接用http://127.0.0.1/test/test这样的请求,实现起来也非常简单,应为PATH_INFO直接就等于$request_uri。有"?"传值时特殊考虑。配置如下。
#add for yiiframework
if (!-e $request_filename){
rewrite (.*) /index.php break;
}
#end for yiiframework
location ~ .*\.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
include fcgi.conf;
fastcgi_pass 127.0.0.1:10080;
fastcgi_index index.php;
set $path_info $request_uri;
if ($request_uri ~ "^(.*)(\?.*)$") {
set $path_info $1;
}
fastcgi_param PATH_INFO $path_info;
}
分享到:
- 2009-10-10 01:01
- 浏览 2809
- 评论(0)
- 论坛回复 / 浏览 (0 / 4119)
- 查看更多
相关推荐
本文是关于 nginx rewrite常用示例。
Nginx的Rewrite规则与实例
nginx rewrite规则 比较apache 和nginx 在rewrite 的不同之处 nginx rewrite的入门之作
实例讲解nginx的rewrite规则
nginx Rewrite语法详解 rewrite规则 常用的命令 if (条件) {} 设定条件,再进行重写 set #设置变量 return #返回状态码 break #跳出rewrite rewrite #重写 Nginx入门到实践-中间件服务、应用层负载均衡、应用层...
#nginx-rewrite彻底解决discz X3多入口问题 #本规则仅适用于nginx。请大家举一反三,根据自己的实际应用情况进行适当调整。有点繁琐,但是有用。 #本文由丽江热线CTO shwan原创,转载请保留本行信息。应用效果见:...
nginx rewrite规则怎么配置-.docx
首先查看下nginx是否支持rewrite: ./nginx -V 不支持说明安装nginx时候缺少pcre,需要重新安装nginx: #安装pcre wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz tar -zxvf pcre-...
Nginx中rewrite实现二级域名、三级域名、泛域名、路径的重写[文].pdf
Yii的Nginx rewrite 如下为nginx yii的重写 server { set $host_path /data/site/www.jb51.net; access_log /data/logs/nginx/www.jb51.net_access.log main; server_name jb51.net www.jb51.net; root $host_path...
主要介绍了Nginx rewrite正则匹配重写的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
导读:nginx rewrite重写规则与防盗链配置方法,rewrite规则格式中flag标记的几种形式,盗链时返回403错误,允许的域名直接跟在第二行的域名后面。 nginx rewrite重写规则与防盗链配置方法如下所示: nginx rewite...
NULL 博文链接:https://eyesmore.iteye.com/blog/1142162
主要介绍了Nginx Rewrite模块应用的几种场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
今天小编就为大家分享一篇nginx rewrite 实现URL跳转的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
Linux中nginx的rewite url重写 总结 有一句话叫:“活到老,学到老,程序员亦是如此”