`
xieye
  • 浏览: 807733 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

nginx下的thinkphp5.1的最佳配置

    博客分类:
  • PHP
阅读更多
nginx下的thinkphp5.1的最佳配置

本文主要参考了:
https://blog.csdn.net/tinico/article/details/18033573
但有两处改动。

特别说明,按照本文的配置,不需要修改php.ini,极为方便。

thinkphp5.1的官网文档推荐的配置:
location / { // …..省略部分代码
   if (!-e $request_filename) {
   rewrite  ^(.*)$  /index.php?s=/$1  last;
    }
}
官网之所以这么写,是想照顾很多老版本的nginx,但新安装linux的同学大可不必如此。

thinkphp5.1官网推荐配置
按照官网要求做的全部配置代码:
server {
    listen   80 ;
    server_name  www.d5.com;
    root /var/www/tptest/public;
    index index.php index.html ;
    location / {
       if (!-e $request_filename) {
           rewrite  ^(.*)$  /index.php?s=/$1  last;
       }
    }
    location ~ \.php {
        include fastcgi_params;
        fastcgi_pass   php72-fpm:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
    }
}

php代码如下:
/var/www/tptest/application/path1/controller/Ctest.php
<?php
namespace app\path1\controller;

class Ctest
{
    public function index()
    {
        return 'ctest ,index';
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name.'<hr>'.var_export( $_GET,1 );
    }
}

使用网址测试:
http://www.d5.com/path1/ctest/hello/name/ppp?aa=33
显示
hello,ppp
array ( 'aa' => '33', )
全部ok

然而,这种写法真的太过时了,

好的 nginx 配置应该是:

server {
    listen   80 ;
    server_name  www.d6.com;
    access_log /var/log/nginx/access.log main2;
    root /var/www/tptest/public;
    index index.php index.html ;
    location / {
       try_files  $uri  /index.php$uri$is_args$args;
    }
    location ~ \.php {
        include fastcgi_params;
        fastcgi_pass   php72-fpm:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_split_path_info  ^(.+\.php)(/.*)$;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
    }
}
如上面的配置文件写法更加直观。try_files 指令实际是对if的包装。之所以nginx推出这个指令,就是希望用户配置时能简单一些。

亲测成功,和参考网址文章的差异是:
1、加了一行SCRIPT_FILENAME 的配置,不加好像不行。
2、try_files那行加了args这个nginx内置变量。

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics