thinkphp5 nginx 虚拟主机配置

想在mac上写一个 thinkphp5 的项目,用的 nginx 服务器,配置了一天各种500404not fond file,活活的在虚拟主机上浪费了一天,如此,将配置贴出来分享给大家。

$ php-fpm -v
PHP 7.0.15 (fpm-fcgi)

$ nginx -v
nginx version: nginx/1.10.3

虚拟主机配置:

server {
    listen 80;
    server_name www.tp5.com;
    root /Users/mac/www/tp5/public;
    index index.php index.html;

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

    location ~ \.php
    {
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        include      fastcgi_params;
        set $path_info "";
        set $real_script_name $fastcgi_script_name;
        if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
            set $real_script_name $1;
            set $path_info $2;
        }
        fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
        fastcgi_param SCRIPT_NAME $real_script_name;
        fastcgi_param PATH_INFO $path_info;
    }
}

参考鸟哥的博文:Nginx(PHP/fastcgi)的PATH_INFO问题

更新 : 2017-6-22

对于 2014 年后的 nginx 都已经支持path_info 模式了。
新的配置

server {
             listen 80;
             server_name www.tp5.com;
             set $root /Users/mac/www/tp5/public;
        
             location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
             {
                 root $root;
             }
        
             location / {
                 root    $root;
                 index    index.html index.php;
                 if ( -f $request_filename) {
                     break;
                 }
                 if ( !-e $request_filename) {
                     rewrite ^(.*)$ /index.php/$1 last;
                     break;
                 }
             }

             location ~ .+\.php($|/) {
                 fastcgi_pass 127.0.0.1:9000;
                 fastcgi_split_path_info ^((?U).+.php)(/?.+)$;    # 支持path_info
                 fastcgi_param PATH_INFO $fastcgi_path_info;
                 fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                 fastcgi_param    SCRIPT_FILENAME    $root$fastcgi_script_name;
                 include        fastcgi_params;
             }
         }

参考:http://www.thinkphp.cn/topic/40391.html

还有一点

在引用 public/static 中的css,js和图片的时候,不能使用/public/static/css/xxx.css 的地址,要去掉 public/static/css/xxx.css 引用,不然会报 404,或者框架报 public 未找到

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,391评论 19 139
  • 一、MemCache简介 session MemCache是一个自由、源码开放、高性能、分布式的分布式内存对象缓存...
    李伟铭MIng阅读 9,270评论 2 13
  • 起由 由于最近的“净网”行动,很多网盘包括我一直在使用的快盘关停,仍然存活的百度云、360云等又没有linux客户...
    leoatchina阅读 12,235评论 1 25
  • 孩子你好: 上封信说了,你要当个自由的人,而自由的人是需要学习的。 爸爸认为,最需要学习的对象应该是工程师—不是因...
    宁长弓阅读 2,256评论 0 2
  • 昨夜读欧阳修浪淘沙 尾句‘纵使花时常病酒,也是风流’ 大感其是,故成词 星汉漾花舟,夜夜中秋。金灯银月十二楼。照影...
    风兮兮__阅读 2,563评论 0 1