前后端分离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;
    }
}
相关推荐
China_Yanhy4 分钟前
我的区块链运维日记 · 第 11 日:生死时速 —— 闪电贷攻击与“红色按钮”
运维·区块链
2401_8401922714 分钟前
怎么把运维能力沉淀下来?
运维
云游云记31 分钟前
nesbot/carbon 常用功能总结
linux·运维·服务器
landonVM1 小时前
Linux 下的高效压缩工具 Zstandard
linux·运维·服务器
遇见火星1 小时前
服务器运维操作命令速查手册
运维·服务器
chengrise1 小时前
Oracle EBS 成本异常排查全指南:差异分摊、成本回滚场景与解决方案
运维·数据库·oracle·erp·ebs
Nightwish51 小时前
Linux随记(二十八)
linux·运维·服务器
Madison-No72 小时前
【Linux】文件操作&&重定向原理
android·linux·运维
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.2 小时前
Haproxy ACL实战:精准分流与访问控制
运维
RockHopper20253 小时前
解读数字化生产运行系统的裁决机制
运维·系统架构·智能制造·isa-95·isa-88