---
title: "(转)Nginx下实现pathinfo及ThinkPHP的URL Rewrite模式支持"
author: "qson"
date: "2014-04-15T13:39:48Z"
tags: []
categories: ["编程语言"]
slug: "nginx-pathinfo-thinkphp-url-rewrite"
draft: false
lastmod: "2014-04-16T13:35:50Z"
id: 2046226
url: "https://qson.iteye.com/blog/2046226"
---

打开Nginx的配置文件 /usr/local/nginx/conf/nginx.conf 一般是在这个路径，根据你的安装路径可能有所变化。如果你配置了vhost，而且只需要你这一个vhost支持pathinfo的话，可以直接打开你的vhost的配置文件。找到类似如下代码（不同版本的nginx可能稍有不同，但是相差不会很远）：


```c

location ~ .*.(php|php5)?$
{
        #原有代码
}

```



```c

#去掉$是为了不匹配行末，即可以匹配.php/，以实现pathinfo
#如果你不需要用到php5后缀，也可以将其去掉
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_param SCRIPT_FILENAME $document_root$real_script_name;
	fastcgi_param SCRIPT_NAME $real_script_name;
	fastcgi_param PATH_INFO $path_info;
}

```


这样，nginx服务器就可以支持pathinfo了。但是如果要支持ThinkPHP的URL_MODE设置为2的模式，还需要配置rewrite规则。找到access_log语句，在其上方加上以下语句：


```c

#如果请求既不是一个文件，也不是一个目录，则执行一下重写规则
if (!-e $request_filename)
{
	#地址作为将参数rewrite到index.php上。
	rewrite ^/(.*)$ /index.php/$1;
	#若是子目录则使用下面这句，将subdir改成目录名称即可。
	#rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
}

```


最后，保存配置文件，重启nginx服务，把ThinkPHP的URL_MODEL设置为2，访问下你的页面，如果能正常访问，恭喜你pathinfo配置成功了。

贴上配置文件：

```c

server {
        listen       80;
        server_name  localhost;
	index index.html index.htm index.php;
	root /home/www;
		
	# think项目 增加过滤功能，支持Rewrite
	location /think {
		# ThinkPHP Rewrite, 除以上指定的静态资源外，其它的请求才有必要进行判断
		if (!-e $request_filename){
			rewrite ^/think/(.*)$ /think/index.php/$1 last;
		}
	}

        #去掉$是为了不匹配行末，即可以匹配.php/，以实现pathinfo
        #如果你不需要用到php5后缀，也可以将其去掉
	location ~ .*\.(php|php5)
	{
		#fastcgi_pass  unix:/tmp/php-cgi.sock;
		fastcgi_pass  127.0.0.1:9000;
		fastcgi_index index.php;
		include fastcgi.conf;

		#定义变量 $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_param SCRIPT_FILENAME $document_root$real_script_name;
                fastcgi_param SCRIPT_NAME $real_script_name;
                fastcgi_param PATH_INFO $path_info;

	}
	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
	{
		expires 30d;
	}
	location ~ .*\.(js|css)?$
	{
		expires 1h;
	}
}

# 日志打印出来，查看请求数据
log_format  _access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" "$http_x_forwarded_for" "$request_filename"';
access_log  /home/log/nginx/access/php.log _access;

```



转：<http://www.thinkphp.cn/topic/3138.html>