配置nginx支持thinkphp框架

因为nginx本身没有支持pathinfo,所以无法使用thinkphp框架,不过我们可以在配置里进行修改使其能够正常使用thinkphp。

1.修改配置支持pathinfo

vi /etc/nginx/cong.d/default.conf

在nginx的配置中添加

location ~ \.php/?.*$ {
      root html;         #这里的路径需要注意一下,自己之前几次配置错误都是因为从网上直接粘贴的路径不对
        fastcgi_pass   127.0.0.1:9000;  
        fastcgi_index  index.php;  
        include        fastcgi.conf;  
                  
        set $fastcgi_script_name2 $fastcgi_script_name;  
        if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {  
            set $fastcgi_script_name2 $1;  
            set $path_info $2;  
        }  
        fastcgi_param   PATH_INFO $path_info;  
        fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name2;  
        fastcgi_param   SCRIPT_NAME   $fastcgi_script_name2;  
    } 

重启nginx,service nginx restart

测试:

修改好了配置之后我们来测试一下

vi /usr/share/nginx/html/think/Application/Home/Controller/IndexController.class.php修改一下控制器文件

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->display();

}
}

再建立模板文件,vi /usr/share/nginx/html/think/Application/Home/View/Index/index.html
随便写一点内容:hello,world

访问地址:###/think/index.php/Home/Index/index.html

显示出hello,world的话,说明配置成功

 

2.我们在thinkphp框架的使用中经常会用到url重写模式,所以我们再配置一下rewrite

location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
        if (!-e $request_filename) {
           rewrite  ^/think/(.*)$  /think/index.php?s=/$1  last;  #因为我的thinkphp项目,放在/usr/share/nginx/html/think的think文件夹下,所以红色部分需要加上否则还是不成功
           break;
        }  
        # example
        #ModSecurityEnabled on;
        #ModSecurityConfig /etc/nginx/modsecurity.conf;
    }
重启nginx

测试:

访问地址:###/think/Home/Index/index.html(注意这里没有index.php也同样可以访问)

界面显示出hello,world

我们的nginx配置成功啦,快去进行愉快的开发吧~~~

转载于:https://www.cnblogs.com/isuifeng/p/5149953.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值