继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Nginx下安装CodeIginter及配置

PIPIONE
关注TA
已关注
手记 921
粉丝 147
获赞 701

CodeIgniter框架安装很简单,只需要到CodeIgniter官网将压缩包下载下来解压,放到服务器根目录即可。

比如,我在nginx服务器根目录下创建一个文件夹ci,并将CodeIgniter框架代码解压到这个文件下。

ci文件夹

ci文件夹下包含上面这些内容。

打开浏览器,输入:http://127.0.0.1/ci/index.php,显示欢迎界面,表示CodeIgniter框架可以使用了。

ci欢迎界面

CodeIgniter框架默认访问welcome控制器下的index方法,上面输出内容就是index方法输出的结果。

下面我们来实现一个自己的控制器,在application/controllers文件夹下创建一个文件Hello.php,即创建一个hello控制器。内容如下:

<?phpdefined('BASEPATH') OR exit('No direct script access allowed');class Hello extends CI_Controller {    public function index() {  
        echo 'I am in hello controller index function!';
    }  

    public function test() {                                                                          
        echo 'I am in  hello controller test function!';
    }  

}

打开浏览器,输入:http://127.0.0.1/ci/index.php/hello/index,由于我们访问的不是默认控制器和方法,必须输入要访问的控制器名和方法名。

访问返回404,但如果使用的是apache服务器时,上面链接可以正常访问。这是因为对于/index.php/hello/index这种url,Apache会按‘/index.php?hello/index’来解释,而nginx认为请求的是index.php/hello/index目录下的文件。所以CodeIgniter框架在nginx下需要特别的配置才可以使用。

打开nginx配置文件,修改之前如下(只显示部分):

···
location / {                 
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
}                         
                              
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000                     location ~ \.php$ {          
        include snippets/fastcgi-php.conf;                    
        # With php5-cgi alone:
        fastcgi_pass 127.0.0.1:9000;        # With php5-fpm:         
        #   fastcgi_pass unix:/var/run/php5-fpm.sock;}  
···

当我们输入http://127.0.0.1/ci/index.php/hello/index地址时,nginx会匹配进入location /块。

location / {                 
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
}

上面location中只有一条命令try_files $uri $uri/ =404;针对上面的访问地址,$uri为/ci/index.php/hello/index,$uri/为/ci/index.php/hello/index/,注意加斜线表示目录,try_files命令首先查找/ci/index.php/hello/index文件是否存在,如果存在直接返回该文件。如果不存在,继续检查/ci/index.php/hello/index/目录是否存在,如果存在直接返回该目录中的index.html或index.php文件,如果目录也不存在就返回404。

为了验证try_files命令是否是这样执行,我们可以在/ci/index.php/hello/index/中创建一个index.html,内容为:This is index.html!。

再输入地址http://127.0.0.1/ci/index.php/hello/index,浏览器显示:

下面我们修改nginx配置,让nginx支持CodeIgniter。其实只要改try_files $uri $uri/ =404;这条命令就可以了,当nginx找不到文件和目录时,不返回404,而是重定向到index.php入口文件。

location /ci/ { 
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /ci/index.php;
}

再输入地址http://127.0.0.1/ci/index.php/hello/index,浏览器显示:

现在nginx可以支持CodeIgniter了。实际上我们也可以http://127.0.0.1/ci/hello/index这样访问,不包含index.php,是不是url简洁很多。

另外上面的配置也可以使用nginx的rewrite命令实现。具体配置如下:

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



作者:zhou
链接:https://www.jianshu.com/p/3852c8975bd8

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP