前后端分离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;
    }
}
相关推荐
虎头金猫几秒前
Beszel 轻量服务器监控:多台服务器状态统一看,搭起来比 Prometheus 省事太多
linux·运维·服务器·分布式·kafka·开源·prometheus
凌晨一点的秃头猪几秒前
在cursor里面设置免密码登录服务器理解
linux·运维·服务器
zt1985q14 分钟前
本地部署搜索引擎 Yacy 并实现外部访问
运维·服务器·网络·网络协议·搜索引擎
不做无法实现的梦~15 分钟前
桌面图标无法隐藏的解决办法
运维·服务器
量子炒饭大师16 分钟前
【Linux系统编程】Cyberpunk在霓虹丛林中构建堡垒 ——【基础开发工具(1)】一文带你初步了解 软件包管理器 并 快速上手 yum和apt 工具
java·linux·运维·apt·yum·软件包管理器
Azure DevOps25 分钟前
在Azure DevOps Server中实现用户端原地址透传(X-Forward-For)
运维·microsoft·azure·devops
草莓熊Lotso25 分钟前
【CMake】 工程实战:可执行文件从编译、链接到安装全流程深度拆解
linux·运维·服务器·网络·c++·cmake
正点原子27 分钟前
【正点原子Linux连载】 第五章 字符设备驱动开发 摘自【正点原子】ATK-DLRK3568嵌入式Linux驱动开发指南
linux·运维·驱动开发
路由侠内网穿透27 分钟前
本地部署开源 HTTP 服务器 OpenLiteSpeed 并实现外部访问
运维·服务器·网络·网络协议·http·开源
源远流长jerry29 分钟前
Linux内核之一条tcp到底占用多少内存
linux·运维·服务器·网络·网络协议·tcp/ip