nginx 部署多个前端项目

  1. 根据域名配置
  2. 根据端口配置
  3. 根据location配置

根据端口配置需要开启双倍的serve 不推荐

根据域名配置

首先域名需要解析到当前的服务器的ip
因为域名是匹配一个公网IP,不带端口号的。
因此,nginx只能通过server_name转发到不同的root目录之下。
nginx默认端口号是80,可以不写,省略。

// 第一个项目
server {
        listen 80;
        server_name a.tl.com; // 申请的域名
        
        location / { 
                root /data/web-a/dist; // 项目地址
                index index.html;
        }
}
// 第二个项目
server {
        listen 80;
        server_name b.tl.com;
        
        location / { 
                root /data/web-b/dist;
                index index.html;
        }
}

根据location配置

// location / 目录是root,其他的要使用alias。
server {
        listen 80;
        
        location / { 
                root /data/web-a/dist; // root
                index index.html;
        }
        
        location /web-b { 
                alias /data/web-b/dist; // alias
                index index.html;
        }
posted @ 2022-07-19 16:51  LC蜗牛  阅读(1289)  评论(0)    收藏  举报