教你在 centos 中配置 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,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]#
这就是前后端分离的流程
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604922
Linux技术交流群:193666693
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!
阅读原文 最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。