lnmp1.2支持ThinkPhp pathinfo及rewrite

时间:2023-03-10 06:22:43
lnmp1.2支持ThinkPhp pathinfo及rewrite

一、pathinfo支持方法

  1.2版本系统已经自动生成了一个pathinfo的配置文件,但实测不可用,所以我们先找打这个文件并修改其内容,文件路径为:/usr/local/nginx/pathinfo.conf,将内容修改如下:

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;

然后找到自己要支持pathinfo网站的配置文件,文件路径为:/user/local/nginx/vhost/**.conf,在文件中找到“#include pathinfo.conf;”这一行,将#去掉。

重启 nginx,命令为:/etc/init.d/nginx restart,重启成功之后即可使用pathinfo方式访问网站。

二、添加rewrite支持方法

  找到要支持网站的配置文件,文件路径为:/user/local/nginx/vhost/**.conf,如果在创建主机时已经设置过重写配置文件,则直接修改之前选择的配置文件,没有的话,可以新建一个other.conf重写配置文件,以后其他网站也可直接使用。

在配置文件中找到"root /home/wwwroot/你的网站根目录",这一句代码,然后在下面加一句“include other.conf;”。

返回nginx的配置目录“/usr/local/nginx/conf/”,如果这里已经存在other.conf则直接覆盖里面的内容,不存在则新建。

other.conf中的内容如下:

location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php/$1;
}
}

  重启nginx,"/etc/init.d/nginx restart",重新访问网站就可以省略index.php了。