当前位置: 首页> 娱乐> 明星 > 前后端分离Nginx

前后端分离Nginx

时间:2025/7/11 1:20:17来源:https://blog.csdn.net/weixin_32128491/article/details/140212904 浏览次数:1次

背景

旧的部署方式是将前端代码打包进后端包的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;}
}
关键字:前后端分离Nginx

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: