前后端分离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;
    }
}
相关推荐
(:满天星:)3 小时前
第31篇:块设备与字符设备管理深度解析(基于OpenEuler 24.03)
linux·运维·服务器·网络·centos
小陶来咯3 小时前
【仿muduo库实现并发服务器】Acceptor模块
运维·服务器
cui_hao_nan3 小时前
Docker后端部署
运维·docker·容器
ZZH1120KQ4 小时前
Linux系统安全及应用
linux·运维·系统安全
小扎仙森4 小时前
关于服务器宝塔转移wordperss子比主题问题
运维·服务器
小小小糖果人4 小时前
Linux云计算基础篇(5)
linux·运维·服务器
我不是哆啦A梦5 小时前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt
weixin_7714323115 小时前
linux系统 weblogic10.3.6(jar) 下载及安装
linux·运维·jar
绝不偷吃5 小时前
FastDFS分布式储存
linux·nginx
scuter_yu5 小时前
主流零信任安全产品深度介绍
运维·网络·安全