Nginx虚拟路径部署

443 阅读1分钟

背景:有时候我们需要在一个项目下部署多个子应用模块,让看起来就像是同一个系统一样,比如http://localhost:3000、http://localhost:3000/admin 的形式。

主应用:http://localhost:3000
子应用:http://localhost:3000/admin

1. 极简模式下的nginx配置

server {
    listen 3000;
    server_name localhost;

    location / {
        root /var/www/mainApp; ###配置主应用的文件夹
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    location /admin { ###配置子应用入口
        alias /var/www/adminApp; ###配置子应用的文件夹
        index index.html index.htm;
        try_files $uri $uri/ /admin/index.html;
    }
}

2. 改造admin子应用的Angular打包配置

打包命令从 ng build --prod 修改为 ng build --prod --base-href /admin/