nginx发布多个前端项目,映射到同一个80端口【前端ruoyi-v3版本】

前提:现在有3个前端项目,分别是80端口,8001端口,8002端口,现在要部署到线上试运行,但是线上只能开放80端口,现在就需要配置nginx,使用nginx进行访问转发。操作如下:

  1. 打包前修改前端代码,添加公共前缀,需要修改2个文件
    (1)vue3的vite.config.js【vue2应该是vue.config.js】
    vite.config.js
    (2)router/index.js【修改路由文件】
    路由文件
  2. 修改nginx配置文件

之前版本的nginx配置文件:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html/ruoyi-ui/dist;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }

        location /prod-api/{
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://ruoyi-gateway:8080/;
        }

        # 避免actuator暴露
        if ($request_uri ~ "/actuator") {
            return 403;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       8001;
        server_name  AAA;

        location / {
            root   /usr/share/nginx/html/AAA/dist;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
            listen       8002;
            server_name  BBB;

            location / {
                root   /usr/share/nginx/html/BBB/dist;
                try_files $uri $uri/ /index.html;
                index  index.html index.htm;
            }


            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
}

修改后的nginx配置文件:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html/ruoyi-ui/dist;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
        location /AAA {
            alias   /usr/share/nginx/html/AAA/dist;
            try_files $uri $uri/ /AAA/index.html;
            index  index.html index.htm;
        }
        location /BBB {
                    alias   /usr/share/nginx/html/BBB/dist;
                    try_files $uri $uri/ /BBB/index.html;
                    index  index.html index.htm;
                }

        location /prod-api/{
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://ruoyi-gateway:8080/;
        }

        # 避免actuator暴露
        if ($request_uri ~ "/actuator") {
            return 403;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

天下文章一大抄,求求你们写点自己的东西吧,这么点东西,我一个后端找半天,抄来抄去的。。

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值