前后端分离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;
    }
}
相关推荐
wanhengidc18 分钟前
云手机存在哪些技术瓶颈
运维·服务器·安全·智能手机·生活
管理大亨21 分钟前
ELK + Redis Docker 企业级部署落地方案
大数据·运维·elk·elasticsearch·docker·jenkins
德迅云安全—珍珍33 分钟前
物理服务器、云服务器、裸金属服务器的区别
运维·服务器
好记忆不如烂笔头abc1 小时前
超微服务器的ipmi调整风扇运行模式
运维·服务器
一个响当当的名号1 小时前
ASP.NET 期末突击
运维·服务器
studytosky1 小时前
Linux 基础开发工具(3):Git 控制与 GDB 调试实用指南
linux·运维·服务器·网络·数据库·git
吕了了1 小时前
给U盘一个PE to Go:将微PE系统直接释放到U盘
运维·windows·电脑·系统
pblh1231 小时前
基于Docker部署测试PySpark
运维·docker·容器
飞翔沫沫情1 小时前
vmware Esxi6.7 root密码重置
运维
良策金宝AI1 小时前
全球工程软件格局重塑:中国AI原生平台的机会窗口
大数据·运维·人工智能