前后端分离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;
    }
}
相关推荐
安红豆.20 分钟前
Linux基础入门 --13 DAY(SHELL脚本编程基础)
linux·运维·操作系统
..空空的人21 分钟前
linux基础指令的认识
linux·运维·服务器
penny_tcf21 分钟前
Linux基础命令halt详解
linux·运维·服务器
万界星空科技1 小时前
界星空科技漆包线行业称重系统
运维·经验分享·科技·5g·能源·制造·业界资讯
荣世蓥1 小时前
10.2 Linux_进程_进程相关函数
linux·运维·服务器
gma9992 小时前
【MySQL】服务器管理与配置
运维·服务器
henan程序媛2 小时前
Jenkins Pipline流水线
运维·pipeline·jenkins
安全不再安全3 小时前
Linux 安装 yum
linux·运维·centos
suri ..3 小时前
【Linux】-----进程第二弹(优先级,环境变量)
linux·运维·服务器
Jay-juice3 小时前
Makefile入门
linux·运维·服务器