教你在 Centos 中配置 Nginx 实现前后端分离

系统 Linux
工作中经常会遇到需要部署前后端分离的项目,今天来给大家介绍一下。实现前后端分离配置,即nginx做代理,前端需要跳转到本地目录访问,后端需要跳转到后端程序。

[[405796]]

工作中经常会遇到需要部署前后端分离的项目,今天来给大家介绍一下。

实验目的

实现前后端分离配置,即nginx做代理,前端需要跳转到本地目录访问,后端需要跳转到后端程序。

服务器:CentOS Linux release 7.9.2009 (Core)

nginx版本:nginx-1.14.2

部署nginx

上传部署包

  1. [root@oracle tools]# ls 
  2. nginx-1.14.2.tar.gz 
  1. [root@oracle tools]# tar xf nginx-1.14.2.tar.gz 
  1. [root@oracle tools]# cd nginx-1.14.2 
  1. [root@oracle nginx-1.14.2]# ./configure 
  1. [root@oracle nginx-1.14.2]# make 
  1. [root@oracle nginx-1.14.2]# make install 

配置前端访问目录

配置nginx配置文件nginx.conf,test为截取到/test/就会跳转到/opt/jingtai/路径

  1. ...  
  2. location  ^~/jingtai/ { 
  3.             alias   /opt/jingtai/; 
  4.             index  index.html index.htm; 
  5.  
  6. ... 

配置后端访问

在配置文件添加一个server

  1. server { 
  2.         listen       8090; 
  3.         server_name  localhost; 
  4.  
  5.         #charset koi8-r; 
  6.  
  7.         #access_log  logs/host.access.log  main; 
  8.  
  9.         location  ^~/dongtai/ { 
  10.             alias   /opt/dongtai/; 
  11.             index  index.html index.htm; 
  12.         } 
  13.  
  14.  } 

在原server添加

  1. upstream dongtai{ 
  2.         server 127.0.0.1:8090; 
  3.     } 
  4.     server { 
  5.         listen       9090; 
  6.         server_name  localhost; 
  7.  
  8.         #charset koi8-r; 
  9.  
  10.         #access_log  logs/host.access.log  main; 
  11.         #jingtai 
  12.         location  ^~/jingtai/ { 
  13.             alias   /opt/jingtai/; 
  14.             index  index.html index.htm; 
  15.         } 
  16.         #dongtai 
  17.         location ^~/dongtai/ { 
  18.                 proxy_pass http://dongtai/; 
  19.         } 

验 证

  • 9090端口代表代理服务和本地前端服务
  • 8090端口代表后端服务
  • 当9090拦截/dongtai/时匹配的是8090端口的路径.
  • 当9090拦截/jingtai/时匹配的是9090/opt/jingtai/的路径。
  1. [root@oracle opt]# curl 127.0.0.1:9090/dongtai/ 
  2. dongtai 
  3. [root@oracle opt]# curl 127.0.0.1:9090/jingtai/ 
  4. jingtai 
  5. [root@oracle opt]# 

这就是前后端分离的流程

 

责任编辑:武晓燕 来源: Linux就该这么学
相关推荐

2019-06-12 19:00:14

前后端分离AppJava

2022-04-06 07:50:57

JWT后端Spring

2023-02-08 16:29:58

前后端开发

2021-09-18 09:45:33

前端接口架构

2014-08-15 10:05:37

Angular权限控制

2017-02-15 10:18:32

架构前后端分离

2020-09-25 11:50:12

前后端分离架构Web

2014-04-18 14:43:07

前后端分离NodeJS

2019-07-09 05:44:35

前后端分离架构接口规范

2021-10-20 18:21:18

项目技术开发

2015-07-01 15:32:39

前端前后端分离

2016-08-22 13:31:05

前端架构前后端分离

2017-11-15 07:01:33

互联网分层架构前后端

2019-12-04 08:44:59

前后端分离开发

2017-10-11 13:25:00

前端

2016-09-21 10:11:19

2022-05-27 10:40:04

前后端权限控制设计

2018-07-20 15:25:02

2017-11-06 08:41:53

互联网分层架构前后端

2015-11-12 10:32:27

前端后端分离
点赞
收藏

51CTO技术栈公众号