您的位置:首页 > 运维架构 > 反向代理

LNMP架构应用实战—Nginx反向代理负载均衡配置

2021-03-24 10:31 1231 查看

               


LNMP架构应用实战—Nginx反向代理负载均衡配置

        前面介绍了nginx虚拟主机的配置,每个虚拟主机提供不同的服务,实际生产环境中,会有多个虚拟主机提供相同的WEB服务,也是通常我们所说的高可用,当其中一台服务器有宕机的情况其它的服务器仍然可以提供正常的WEB服务,因此,就会用到前端的负载均衡器,进行负载调度,负载均衡有硬件设备(F5)、软件等,今天就来介绍下nginx HTTP反向代理负载均衡的功能


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

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


2、整体逻辑图

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

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

[strong][/strong]

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


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


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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: