工作中经常会遇到需要部署前后端分离的项目,今天来给大家介绍一下。
实验目的
实现前后端分离配置,即nginx做代理,前端需要跳转到本地目录访问,后端需要跳转到后端程序。
服务器:CentOS Linux release 7.9.2009 (Core)
nginx版本:nginx-1.14.2
部署nginx
上传部署包
[root
@oracle
 tools]# ls

nginx-
1.14
.2.tar.gz

[root
@oracle
 tools]# tar xf nginx-
1.14
.2.tar.gz

[root
@oracle
 tools]# cd nginx-
1.14
.2

[root
@oracle
 nginx-
1.14
.2]# ./configure

[root
@oracle
 nginx-
1.14
.2]# make

[root@oracle nginx-1.14.2]# make install
配置前端访问目录
配置nginx配置文件nginx.conf,test为截取到/test/就会跳转到/opt/jingtai/路径
... 

location  ^~/jingtai/ {

            alias   /opt/jingtai/;

            index  index.html index.htm;


...
配置后端访问
在配置文件添加一个server
server {

        listen       
8090
;

        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location  ^~/dongtai/ {

            alias   /opt/dongtai/;

            index  index.html index.htm;

        }


 }
在原server添加
upstream dongtai{

        server 
127.0
.0.1:
8090
;

    }

    server {

        listen       
9090
;

        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;

        #jingtai

        location  ^~/jingtai/ {

            alias   /opt/jingtai/;

            index  index.html index.htm;

        }

        #dongtai

        location ^~/dongtai/ {

                proxy_pass http:
//dongtai/;
        }

验    证
  • 9090端口代表代理服务和本地前端服务
  • 8090端口代表后端服务
  • 当9090拦截/dongtai/时匹配的是8090端口的路径.
  • 当9090拦截/jingtai/时匹配的是9090/opt/jingtai/的路径。
[root
@oracle
 opt]# curl 
127.0
.0.1:
9090
/dongtai/

dongtai

[root
@oracle
 opt]# curl 
127.0
.0.1:
9090
/jingtai/

jingtai

[root
@oracle
 opt]#
这就是前后端分离的流程
END
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604922
Linux技术交流群:193666693
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!
继续阅读
阅读原文