实验目的:
实现前后端分离配置,即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】#
结束
这就是前后端分离的流程
本文原创地址:https://www.linuxprobe.com/nginx-separation-of-front.html
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.