目录

nginx部署多个前端项目基于location配置

目录

nginx部署多个前端项目(基于location配置)

基于loaction配置;同端口,同域名,不同的前端项目目录;

项目1放在/www/dist目录下;

项目2放在/www/dist2目录下;

server {
       listen 8888 default_server;   
       server_name localhost;
        location / {
            root /www/dist;
            index index.html;
        }
        location /edu {
            alias /www/dist2;
           index index.html;
        }
        location /vod {
            alias /www/dist;
            index index.html;
        }
    }    

location / 目录是root;其他的要使用alias;

访问预览结果:

https://i-blog.csdnimg.cn/blog_migrate/ab92e0bcffd2275a7da0ed0e1b1be77f.png

https://i-blog.csdnimg.cn/blog_migrate/5f639d7b6fc1176da5dd973ab51227aa.png

React项目配置时参考博客:

1). 要在package.json中添加homepage属性;(绝对路径)

https://i-blog.csdnimg.cn/blog_migrate/789f321a84b16743ace745a29b678646.png

添加homepage属性后,在打包生成的index.html入口文件中,引入的静态资源文件会加上ip后面的内容;这里会在引入的资源链接前添加上/edu 前缀

https://i-blog.csdnimg.cn/blog_migrate/0ba21480ff42169e26e69b98ae85bd5f.png

2). 在 BrowserRouter/HashRouter 元素里面加basename属性;

basename配置参考:

https://i-blog.csdnimg.cn/blog_migrate/7667f8dda162c0490468b755fc515be6.png

全文配置参考博客: