前后端分离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;
    }
}
相关推荐
贵州诚鑫致达科技有限公司6 小时前
Linux的yum源怎么重启?源不是服务
linux·运维·服务器
遇印记7 小时前
传输层,网络层
运维·服务器·网络·windows·学习
品牌常新7 小时前
景区巡逻机器人怎么选?派宝S5全地形守护解析
运维·人工智能·机器人
Elastic 中国社区官方博客7 小时前
一条 ES|QL 查询,涵盖365天的审计日志:在热数据和可搜索快照上执行相同的搜索
大数据·运维·服务器·elasticsearch·搜索引擎·全文检索
澜山奕秋9 小时前
RHCSA
linux·运维·服务器
cnnews10 小时前
Chromebox3安装Ubuntu后内存化与最小化 eMMC 写入优化
linux·运维·ubuntu
AliCloudROS10 小时前
OOS ChatOps Agent 定时任务:每天 9 点,让Agent 在钉钉群里交运维报告
运维·自动化运维
CALL1581190984510 小时前
SVHC 247项与附录XVII限制物质,REACH测试项目有哪些?
运维·安全·安全性测试
flytalei10 小时前
Ubuntu 24.04 单节点 K3s 部署实践:从环境评估到故障排查与验收
linux·运维·ubuntu