ngin负载均衡集群(一)

一、nginx负载均衡集群介绍:

1.反向代理与负载均衡概念简介
严格地说, nginx仅仅是作为 Nginx Proxy反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果,所以本文称之为nginx负载均衡。那么,反向代理和负载均衡有什么区别呢?
普通负载均衡软件,例如大名鼎鼎的LVS,其实现的功能只是对请求数据包的转发(也可能会改写数据包)、传递,其中DR模式明显的特征是从负载均衡下面的节点服务器来看,接收到的请求还是来自访问负载均衡器的客户端的真实用户,而反向代理就不样了,反向代理接收访问用户的请求后,会代理用户重新发起请求代理下的节点服务器,最后把数据返回给客户端用户,在节点服务器看来,访问的节点服务器的客户端用户就是反向代理服务器了,而非真实的网站访问用户。句话,LVS等的负载均衡是转发用户请求的数据包,而 nginx反向代理是接收用户的请求然后重新发起请求去请求其后面的节点。

2、实现负载均衡的组件说明:

实现负载均衡的组件主要有两个:

ngx_http_proxy_module           proxy代理模块,用于把请求后抛给服务器节点或upstream服务器池
ngx_http_upstream_module        负载均衡模块,可以实现网站的负载均衡功能及结点的健康检查 

 二、环境准备:

系统:CentOS Linux release 7.5.1804 (Core)
LB01    192.168.100.105        nginx主负载均衡器
LB02    192.168.100.106        nginx辅负载均衡器
Web01    192.168.100.107        Web01服务器
Web02    192.168.100.108        Web02服务器

nginx版本:1.8.1

三、安装nginx软件
在以上4台服务器上安装nginx

编译安装nginx请参考:https://www.cnblogs.com/Mr-Ding/p/9502529.html

nginx启动脚本参考:https://www.cnblogs.com/Mr-Ding/p/9502972.html

四、配置用于测试的web服务

nginx web01和web02配置如下:

cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
		access_log logs/access.log main;
    }
}

重启服务:
systemctl reload nginx

web01下面写入:
[root@web01 conf]# echo "192.168.100.107" > ../html/index.html

web02下面写入:
[root@web02 conf]# echo "192.168.100.108" > ../html/index.html


配置hosts:

web01:
[root@web01 conf]# tail -1 /etc/hosts
192.168.100.107	www.dmtest.com

web02:
[root@web02 conf]# tail -1 /etc/hosts
192.168.100.108 www.dmtest.com

测试:
[root@web01 conf]# curl www.dmtest.com
192.168.100.107

[root@web02 conf]# curl www.dmtest.com
192.168.100.108

五、 实现一个简单的负载均衡

在LB01上作如下操作:
[root@lb01 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
	upstream www_server_pools   {		#这里是定义web服务器池,包含了107和108两个web节点;
        server 192.168.100.107:80   weight=1;
        server 192.168.100.108:80   weight=1;
    }
    server {		#这里是定义代理的负载均衡域名虚拟主机;
        listen       80;
        server_name  www.dmtest.com;
        location / {
        proxy_pass http://www_server_pools;		#访问www.dmtest.com,请求发送给www_server_pools里面的节点;
        }

        }
    }

检查语法并重启nginx服务
[root@lb01 conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
[root@lb01 conf]# systemctl restart nginx

测试:
[root@lb01 conf]# tail -1 /etc/hosts
192.168.100.105		www.dmtest.com

[root@lb01 conf]# curl www.dmtest.com
192.168.100.107

[root@lb01 conf]# curl www.dmtest.com
192.168.100.108

[root@lb01 conf]# curl www.dmtest.com
192.168.100.107

[root@lb01 conf]# curl www.dmtest.com
192.168.100.108
原文地址:https://www.cnblogs.com/Mr-Ding/p/9644006.html