六月 13th, 2010

thinkphp + nginx 实现url(rewrite) 伪静态

近日公司做一个项目用thinkphp 做程序框架,nginx做webserver,其中要用到url伪静态规则。

如:http://www.ys250.com/2010/06/13/thinkphp_nginx_rewrite/

1.在thinkphp的Conf/config.php中, 添加 URL_MODEL 等于 2, 使用REWRITE模式

return array(
	//'配置项'=>'配置值'
	'DEBUG_MODE' => true,
	'URL_MODEL' => 2,//启用REWRITE模式
	'APP_DOMAIN_DEPLOY'     => true,
	//'URL_HTML_SUFFIX'=>'.html',
	//'URL_ROUTER_ON' => true
);

2.nginx.conf 的配置如下:

 
#user  nobody;
worker_processes  1;
 
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    sendfile        on;
    keepalive_timeout  65;
 
    server {
        listen       88;
        server_name  localhost;
 
        charset utf-8;
 
	index  index.html index.htm index.php;
 
	root   d:/nginx/html;
	autoindex on;
 
	location / {
		    index  index.php;
		    if (!-e $request_filename) {
			#将所有找不到的文件或者目录请求转发到index.php 
			rewrite  ^(.*)$  /index.php/$1  last;
			break;
		    }
		}
	#能找到的php,都让fastcgi去处理. 
	location ~ .php$ {
	     fastcgi_pass   127.0.0.1:9000;
	     fastcgi_index  index.php;
	     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 
	     include fastcgi_params;
	}
 
	#将index.php转发到fastcgi,增加了若干参数
	location ~ index\.php($|/) {
	    set $script     $uri;
	    set $path_info  "/";
	    if ($uri ~ "^(.+\.php)(/.+)") {
	       set $script     $1;
	       set $path_info  $2;
	    }
 
	    fastcgi_pass   127.0.0.1:9000;
	    fastcgi_index  index.php?IF_REWRITE=1;
	    include fastcgi_params;
	    fastcgi_param PATH_INFO $path_info;
	    fastcgi_param  SCRIPT_FILENAME  d:/nginx/html/index.php;
	    fastcgi_param SCRIPT_NAME "";
	}
 
    }
 
}

nginx.conf 配置参考自:http://ifphp.cn/document-2.x/p/guide/nginx_with_if

Write A Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">