thinkphp5 nginx 虚拟主机配置

2017-06-10  本文已影响0人  xiaFeng

想在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 未找到

上一篇下一篇

猜你喜欢

热点阅读