温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LNMP架构中Nginx反向代理负载均衡如何配置

发布时间:2021-12-07 14:54:04 来源:亿速云 阅读:135 作者:小新 栏目:云计算

这篇文章主要介绍LNMP架构中Nginx反向代理负载均衡如何配置,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!


1、配置环境介绍

系统环境:

[root@centos6 conf]# cat /etc/redhat-release 

CentOS release 6.5 (Final)

[root@centos6 conf]# uname -r

2.6.32-431.el6.x86_64

nginx版本:

[root@centos6 conf]# /application/nginx/sbin/nginx -v

nginx version: nginx/1.10.1

LNMP架构中Nginx反向代理负载均衡如何配置

LNMP架构中Nginx反向代理负载均衡如何配置

配置二台虚拟主机,用来做后续测试



2、整体逻辑图

LNMP架构中Nginx反向代理负载均衡如何配置


说明:当用户访问时,其实访问的是负载均衡器对外提供的地地址,然后由它来根据相应的规则进行转发给后端后服务器


3、配置过程

[root@centos6 conf]# vi nginx.conf

worker_processes  1;    

events {                          

    worker_connections  1024;    

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65; 

include /application/nginx/conf/extra/upstream01.conf;

  }

增加上述配置即可


接下来配置upstream01.conf


[root@centos6 extra]# vi upstream01.conf

#####config to nginx 

upstream test_servers {      #定义主机池

server 172.16.1.235:8081 weight=5;  #按权重的方式进行轮询

server 172.16.1.235:8080 weight=5; 

server 172.16.1.235:80 weight=15;

      }

server { 

listen 80; 

server_name www.mingonge.com;

location / { 

 proxy_pass http://test_servers;     #将监听到请求转发到这个虚拟主机池

}  

 }


更多关于upstream模块的介绍,请参考官方文档

http://nginx.org/en/docs/http/ngx_http_upstream_module.html


4、重启服务并测试

重启nginx服务

[root@centos6 extra]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.10.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.10.1/conf/nginx.conf test is successful

[root@centos6 extra]# /application/nginx/sbin/nginx -s reload

linux本地客户端测试

[root@centos6 extra]# curl http://172.16.1.235

welcont to mingongge's blog stie

[root@centos6 extra]# curl http://172.16.1.235

welcont to mingongge's bbs stie

[root@centos6 extra]# curl http://172.16.1.235

welcont to mingongge's blog stie

用户客户端用域名测试

本地客户端需要将域名正确解析,www.mingongge.com------>172.16.1.235

LNMP架构中Nginx反向代理负载均衡如何配置

LNMP架构中Nginx反向代理负载均衡如何配置


从上面的测试结果来看,的确两次访问分配的服务器是不同的,为了测试效果,所以将显示的内容配置成不同,实际生产环境中,所有的访问显示内容都是一样的,实现服务器宕机但不会影响用户的体验度



5、模拟测试真实环境

我们这里将两台虚拟机首页内容配置成相同显示内容来模拟真实生产环境

[root@centos6 ~]# echo "welcome to mingongge's web site" >/www/bbs/index.html

[root@centos6 ~]#  echo "welcome to mingongge's web site" >/www/blog/index.html

[root@centos6 ~]# cat /www/bbs/index.html 

welcome to mingongge's web site

[root@centos6 ~]# cat /www/blog/index.html 

welcome to mingongge's web site

linux客户端测试

[root@centos6 ~]# curl http://172.16.1.235

welcome to mingongge's web site

[root@centos6 ~]# curl http://172.16.1.235

welcome to mingongge's web site


停止其中一台虚拟的WEB服务功能来模拟故障,由于是用的nginx本身的虚拟主机,这里我们就修改配置文件,将包含配置文件注释掉

#include  /application/nginx/conf/extra/vhosts/bbs.conf;

如果测试用三台服务器,可以配置不同的http服务,模拟服务器宕机(停止WEB服务),来测试负载均衡的效果更加贴近现实环境


[root@centos6 ~]# curl http://www.mingongge.com        

welcome to mingongge's web site

[root@centos6 ~]# curl http://www.mingongge.com

welcome to mingongge's web site

LNMP架构中Nginx反向代理负载均衡如何配置
客户端仍然可以访问 ,表明负载均衡的功能是生效的,当其中的服务器出现宕机情况,也不会影响用户的最终访问

以上是“LNMP架构中Nginx反向代理负载均衡如何配置”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI