Nginx负载均衡反向代理动静分离

编程入门 行业动态 更新时间:2024-10-11 01:13:51

Nginx负载均衡反向代理<a href=https://www.elefans.com/category/jswz/34/1668035.html style=动静分离"/>

Nginx负载均衡反向代理动静分离

文章目录

  • nginx负载均衡&反向代理&动静分离
        • 环境
        • 说明
        • 部署动静分离
          • 1.主机lnmp部署一个动态页面,在此以discuz论坛系统为例
          • 2.主机n1部署两个静态页面
          • 访问动、静态页面
        • 配置负载均衡
        • 配置反向代理
        • 访问测试

nginx负载均衡&反向代理&动静分离

环境
主机名角色环境操作系统IP地址
lb负载均衡器nginx/1.24.0centos-8192.168.179.10
lamp动态网站服务器lnmp架构+Discuz论坛centos-8192.168.179.11
n1静态网站服务器nginx/1.24.0centos-8192.168.179.100

说明

主机lamp部署一个动态网页,主机n1部署一个静态页面。主机lb部署nginx服务,实现动静分离的负载均衡

部署nginx服务、部署lnmp架构请阅读nginx服务和LNMP架构&部署Discuz论坛系统

部署动静分离
1.主机lnmp部署一个动态页面,在此以discuz论坛系统为例

部署lnmp架构&discuz论坛请阅读和LNMP架构&部署Discuz论坛系统

//配置访问根目录就访问到论坛
//修改配置文件默认的server下面的两个localtion
[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf
......location / {root   html/Discuz/upload;             //改为论坛的根目录index  index.php index.html index.htm;}
......location ~ \.php$ {root           html/Discuz/upload;    //改为论坛的根目录fastcgi_pass   127.0.0.1:9000;fastcgi_index  index.php;fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;include        fastcgi.conf;}
......//重启nginx服务
[root@lnmp ~]# systemctl restart nginx.service 
2.主机n1部署两个静态页面
1.第一个静态页面
//创建一个目录,并编辑一个测试用的index.html文件
[root@n1 ~]# cd /usr/local/nginx/html/
[root@n1 html]# mkdir www.test1
[root@n1 html]# vim www.test1/index.html
[root@n1 html]# cat www.test1/index.html 
this is test1//修改配置文件,创建第1个虚拟主机
[root@n1 ~]# vim /usr/local/nginx/conf/nginx.conf
......
server {listen       80;server_name  www.test1;         //第一个域名
......
location / {root   html/www.test1;      //修改目录为网站文件的目录index  index.html index.htm;}
......2.第二个静态页面
//创建一个目录,并编辑一个测试用的index.html文件
[root@n1 ~]# cd /usr/local/nginx/html/
[root@n1 html]# mkdir www.test2
[root@n1 html]# vim www.test2/index.html
[root@n1 html]# cat www.test2/index.html 
this is test2
[root@n1 html]# //修改配置文件,创建第2个虚拟主机......server {listen       8080;server_name  www.test2;location / {root   html/www.test2;index  index.html index.htm;}}
......//重启nginx服务
[root@n1 ~]# systemctl restart nginx.service 
访问动、静态页面

访问第一个静态

访问第二个静态



访问动态


配置负载均衡

在负载均衡器(主机lb)里配置

//修改配置文件,在http段里面写(与server平级)
[root@lb ~]# vim /usr/local/nginx/conf/nginx.conf
......upstream dynamic {server 192.168.179.11:80 weight=1;        //动态页面}upstream static {server 192.168.179.100:80 weight=1;       //静态页面,有两个,做负载均衡server 192.168.179.100:8080 weight=1;}
......
配置反向代理

在负载均衡器(主机lb)里配置

在server段里面配,和localtion同级

1.//配置访问根目录就是访问静态页面
[root@lb ~]# vim /usr/local/nginx/conf/nginx.conf
......location / {proxy_pass http://static;    //访问根就跳转到静态页面}
......2.//配置访问.php的就是访问动态页面//找到这三行,取消注释,修改
[root@lb ~]# vim /usr/local/nginx/conf/nginx.conf
......location ~ \.php$ {proxy_pass   http://dynamic;}
......//重启服务
[root@lb ~]# nginx -s stop
[root@lb ~]# nginx 
访问测试

访问负载均衡器(主机lb)的ip,反向代理到静态页面

访问成功,刷新一下

成功实现负载均衡


访问负载均衡器(主机lb)的ip加index.php,反向代理到动态页面

访问成功

因为负载均衡器本地没有动态页面的文件,所以没有图片显示

更多推荐

Nginx负载均衡反向代理动静分离

本文发布于:2023-12-06 10:38:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1667318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动静   负载均衡   Nginx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!