登录
原创

多个 Laravel 项目的 Nginx 统一配置

专栏苏式汤面 Laravel
发布于 2021-03-22 阅读 1075
  • Laravel
原创

业务场景

abc.com 下有多个项目,都使用 Laravel 来开发,而且将来还会有更多的项目增加进来,如下表所示:

项目域名 部署路径 用途
exp.abc.com /wwwroot/exp.abc.com 快递API服务
ip.abc.com /wwwroot/ip.abc.com IP地址查询API服务
poi.abc.com /wwwroot/poi.abc.com POI查询API服务
… 将来增加的其他项目

需求

每个项目的 Nginx 配置,除了域名和项目路径,其他都是一致的。
如果每增加一个项目都要增加 Nginx 配置,配置文件的数量是巨大的,后期将难以维护。
是否有一劳永逸的办法?

解决方案

只要一个Nginx配置文件即可,将来增加的其他项目也能正常运行,无需改动。
Nginx 配置的核心代码如下:


http {

    # 关键配置
    map $host $dir {
        ~^(.*)$ /Home/user/laravel/$1/public;
    }

    server {
        listen 80;

        server_name  _;
        root $dir;

        location / {
            index index.html index.htm index.php;
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
            fastcgi_param  QUERY_STRING       $query_string;
            include        fastcgi_params;
        }

        location = /debug {
            default_type text/plain;
            return 200 $dir;
        }
    }
}

评论区

admin
14粉丝

打江山易,守江山难,负重前行,持续创新。

0

0

0

举报