配置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 HomeController;
use ThinkController;
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