技术交流QQ群:①185473046   ②190706903   ③203744115   网站地图
登录

下次自动登录
现在的位置: 首页Nginx>正文
Nginx下配置支持ThinkPHP的pathinfo模式
2016年11月28日 Nginx 暂无评论 ⁄ 被围观 13,149次+

说明:

Nginx目录:/usr/local/nginx/

Nginx配置文件:/usr/local/nginx/nginx.conf

如果站点使用了vhost虚拟主机,并且只需要这一个虚拟主机支持pathinfo的,可以直接打开你的vhost的配置文件进行设置(绿色字为修改代码蓝色字为增加代码)。

找到类似如下代码:

location ~ .*\.(php|php5)?$

{

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

#原来的代码

......

}

编辑配置文件为以下代码:

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;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

#原来的代码

......

}

这样设置后,nginx已经可以支持pathinfo了。如果要支持ThinkPHP的URL_MODE设置为2的模式(隐藏index.php),还需要配置rewrite规则。找到access_log语句,在其上方加上以下语句:

location / {

if (!-e $request_filename) {

rewrite ^(.*)$ /index.php?s=$1 last;

break;

}

}

#如果ThinkPHP安装在二级目录,Nginx的伪静态方法设置如下,其中subdir是所在的目录名称

location /subdir/ {

if (!-e $request_filename){

rewrite ^/subdir/(.*)$ /subdir/index.php?s=$1 last;

}

}

最后,重启Nginx服务,service nginx restart 使配置生效。

至此,Nginx下配置支持ThinkPHP的pathinfo模式教程完成。

     

  系统运维技术交流QQ群:①185473046 系统运维技术交流□Ⅰ ②190706903 系统运维技术交流™Ⅱ ③203744115 系统运维技术交流™Ⅲ

给我留言

您必须 [ 登录 ] 才能发表留言!



Copyright© 2011-2024 系统运维 All rights reserved
版权声明:本站所有文章均为作者原创内容,如需转载,请注明出处及原文链接
陕ICP备11001040号-3