乐趣区

关于centos:centos79-配置nginx实现前后端分离

工作中常常会遇到须要部署前后端拆散的我的项目,明天来给大家介绍一下。
centos7.9 配置 nginx 实现前后端拆散 centos7.9 配置 nginx 实现前后端拆散
试验目标:
实现前后端拆散配置,即 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, 截取到 /jingtai/ 就会跳转到 /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]#
完结
这就是前后端拆散的流程

退出移动版