• 配置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配置成功啦,快去进行愉快的开发吧~~~

  • 相关阅读:
    《自拍教程17》Python调用命令
    c和c++学哪个?
    PHP:变量之效果域、静态变量,常量等基础知识
    Java中NIO及基础实现
    零代码=零门槛?
    程序员真的都比较宅吗?
    DataGridView怎样完成添加、删除、上移、下移一行
    C# 控件 RichTextBox 显示行号,而且与Panel彼此联动
    C语言代码中的空白符表示什么
    php 中的4种标记风格介绍
  • 原文地址:https://www.cnblogs.com/isuifeng/p/5149953.html
Copyright © 2020-2023  润新知