前后端分离Nginx

背景

旧的部署方式是将前端代码打包进后端包的resource

复制代码
 server {
    listen       80;
    listen       443 ssl;
    server_name  xxx.test.com;
    
    location / {
         proxy_pass  http://xxx.test.com;
    }
}

后端:https:// xxx.test.com/simcard/querySimcard

前端:https:// xxx.test.com/#/app/number/card-query/list

问题

前端的代码是作为后端的一部分,无法独立发布

改造要求

1.保持后端接口访问路由不变

2.没有额外域名提供

3.尽量不改动或者少量改动现有代码

不满足要求(常规)

复制代码
 server {
    listen       80;
    listen       443 ssl;
    server_name  xxx.test.com;
    
    location / {
         root /usr/local/nginx/html/test-html/test-web;
         index index.html index.htm;
    }
    location /api {
         proxy_pass  http://xxx.test.com;
    }
}

缺点

1.导致后端访问接口路由改变

满足要求方案

1.后端增加一个访问入口

复制代码
@Controller
public class IndexController {
    @GetMapping(value = { "/", "/index", "/index.html" })
    public void index(HttpServletResponse response) throws IOException {
        response.sendRedirect("http://xxx.test.com/index.html");
    }
}

2.Nginx代理前后端

复制代码
 server {
    listen       80;
    listen       443 ssl;
    server_name  xxx.test.com;
    # 前端静态资源    
    location /dist/ {
         alias /usr/local/nginx/html/test-html/test-web;
         autoindex on;            
    }
    # 前端index页面,通过后端入口访问转发
    location /index.html {
         alias /usr/local/nginx/html/test-html/test-web;
         index index.html index.htm;
    }
    # 后端接口代理
    location / {
         proxy_pass  http://xxx.test.com;
    }
}
相关推荐
Leo.yuan12 分钟前
数据湖是什么?数据湖和数据仓库的区别是什么?
大数据·运维·数据仓库·人工智能·信息可视化
程序员JerrySUN33 分钟前
Linux 内核内存管理子系统全面解析与体系构建
java·linux·运维
CIb0la43 分钟前
Ai自动补全编程工具:llama vscode
运维·开发语言·学习·测试工具·程序人生
rui锐rui1 小时前
大模型模型部署和暴露接口
linux·运维·服务器
BJ_Bonree1 小时前
博睿数据×华为, 共筑智慧金融新未来
运维
孙克旭_1 小时前
day030-Shell自动化编程-函数
linux·运维·自动化
IT葛大侠1 小时前
OSPF域内路由
运维·网络·计算机网络
面朝大海,春不暖,花不开3 小时前
管理数据洪流:自动化处理与归档每日数据文件的策略与实践
运维·python·自动化
码农101号11 小时前
Linux中shell编程表达式和数组讲解
linux·运维·服务器
powerfulzyh11 小时前
非Root用户启动SSH服务经验小结
运维·ssh