前后端分离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;
    }
}
相关推荐
kyle~2 小时前
机器视觉---Intel RealSense SDK 2.0 开发流程
运维·c++·windows·深度相机·intel realsense
数智大号2 小时前
超云发布R2425存储服务器:以全栈自研引领国产存储新方向
运维·服务器
亚林瓜子2 小时前
在amazon linux 2023上面通过Fedora 36软件仓库源安装tesseract5
linux·运维·服务器·ocr·tesseract·amazon·fedor
是专家不是砖家2 小时前
linux USB摄像头不停掉线问题
linux·运维·服务器
yuanManGan3 小时前
走进Linux的世界:初识进程(Task)
linux·运维·服务器
小马哥编程3 小时前
【软考架构】案例分析-瘦客户端C/S架构
运维·服务器·架构
老黄编程3 小时前
09-ubuntu20.04 执行 apt update时报错,是因为官网已停止维护不再更新的缘故吗?
linux·运维·服务器·ubuntu·数字证书
Supernova_Jun3 小时前
ffmpeg图片转视频
linux·运维·服务器
水月wwww3 小时前
ubuntu网络连接出错解决办法
linux·运维·计算机网络·ubuntu·操作系统·ubuntu网络连接
0wioiw03 小时前
Ubuntu(①shell脚本)
linux·运维·ubuntu