`

(转)Nginx下实现pathinfo及ThinkPHP的URL Rewrite模式支持

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

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


#去掉$是为了不匹配行末,即可以匹配.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语句,在其上方加上以下语句:

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


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

贴上配置文件:
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
分享到:
评论

相关推荐

    Nginx下配置pathinfo及ThinkPHP的URL Rewrite模式支持

    主要介绍了Nginx下配置pathinfo及ThinkPHP的URL Rewrite模式支持,使用Nginx运行ThinkPHP的必备配置,需要的朋友可以参考下

    Ubuntu下Nginx配置ThinkPHP的Pathinfo和URl Rewrite模式

    主要介绍了Ubuntu下Nginx配置ThinkPHP的Pathinfo和URl Rewrite模式,Ubuntu下的配置会有一些不同之处,需要的朋友可以参考下

    Nginx下支持Thinkphp URL Rewrite的配置示例

    现在做项目大多数时候都是在使用ThinkPHP,但是Nginx默认不支持ThinkPHP的pathinfo模式,需要进行一定的配置。 Nginx配置文件 # # The default server # server { listen 80 default_server; #server_name ...

    让Nginx支持ThinkPHP的URL重写和PATHINFO的方法分享

    ThinkPHP支持通过PATHINFO和URL rewrite的方式来提供友好的URL,只需要在配置文件中设置 'URL_MODEL' => 2 即可。在Apache下只需要开启mod_rewrite模块就可以正常访问了,但是Nginx中默认是不支持PATHINFO的,所以...

    thinkphp在低版本Nginx 下支持PATHINFO的方法分享

    最近在用thinkphp做一个项目,基本完成后部署到nginx服务器上才发觉nginx是不支持pathinfo的那么我们如何来处理呢。 Nginx环境 在Nginx低版本中,是不支持PATHINFO的,但是可以通过在Nginx.conf(在/usr/local/nginx...

    Nginx配置PATHINFO隐藏thinkphp index.php

    Nginx配置PATHINFO隐藏index.php Nginx配置文件里放入这段代码 server { listen 80; default_type text/plain; root /var/www/html; index index.php index.htm index.html; #隐藏index.php location / { if...

    nginx+thinkphp下解决不支持pathinfo模式

    我这种方式是不需要将URL_Model改为rewrite/兼容的: 大约第43行,按照下面的对照自己的code进行修改,请注意细节的符号: location / { root C:/Zend/workspaces; #//这是你自己项目的根目录。 index index

    nginx下配置thinkphp文件的方法

    在上篇文章给大家介绍了在Nginx上部署ThinkPHP项目教程,今天给大家介绍nginx下thinkphp的配置,具体详解如下: ## domain redirect #if ($host != my.ruanzhuangyun.cn){ # rewrite ^/(.*)$ ...

    wpsshop官方正版5.0.2 b2c 商城 thinkphp (php开源项目)

    Wpsshop商城系统 - ... 在Nginx低版本中,是不支持PATHINFO的,但是可以通过在Nginx.conf中配置转发规则实现: location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } }

    ThinkPHP框架里隐藏index.php

    本文所写的配置在ThinkPHP3.2.2上测试过。按理也兼容其它版本。 首先修改配置文件: ... 3 (兼容模式) 默认为PATHINFO 模式 Nginx 推荐: location / { try_files $uri $uri/ /index.php?s=$u

    wpsshop官方正版5.0.1 thinkphp框架 手机端 pc端 官方1.9万

    Wpsshop商城系统 - ... 在Nginx低版本中,是不支持PATHINFO的,但是可以通过在Nginx.conf中配置转发规则实现: location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } }

Global site tag (gtag.js) - Google Analytics