首页 > 代码库 > Nginx负载均衡

Nginx负载均衡

  • 环境准备

四台服务器、一台客户机、一台nginx负载均衡器、三台web(其中一台web主机为备节点)

技术分享

  • 安装软件

三台web(使用nginx或apache)

web01

[root@web01 /]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

[root@web01 /]# yum -y install httpd
[root@web01 /]# vim /etc/httpd/conf/httpd.conf 
ServerName  www.web01.com:80
[root@web01 /]# echo "web01_192.168.119.130" > /var/www/html/index.html
[root@web01 /]# /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]
[root@web01 /]# curl 192.168.119.130
web01_192.168.119.130
[root@web01 /]# 
[root@web01 /]# curl 192.168.119.130
web01_192.168.119.130

 

web02

[root@web02 /]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

[root@web02 /]# yum -y install httpd
[root@web02 /]# vim /etc/httpd/conf/httpd.conf 
ServerName  www.web02.com:80
[root@web02 /]# echo "web02_192.168.119.133" > /var/www/html/index.html
[root@web02 /]# /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]
[root@web02 /]# curl 192.168.119.133
web02_192.168.119.133

 

web_backup

[root@web_backup /]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

[root@web_backup /]# yum -y install httpd
[root@web_backup /]# vim /etc/httpd/conf/httpd.conf
ServerName www.web03.com:80
[root@web_backup /]# echo "web03_backup" > /var/www/html/index.html
[root@web_backup /]# /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]
[root@web_backup /]# curl 192.168.119.131
web03_backup

 

LB nginx负载均衡器

[root@lb01 /]#wget http://nginx.org/download/nginx-1.6.3.tar.gz
[root@lb01 /]#yum -y install pcre pcre-devel
[root@lb01 /]#yum -y install gcc gcc-c++
[root@lb01 /]#yum -y install openssl openssl-devel
[root@lb01 /]#tar zxvf nginx-1.6.3.tar.gz
[root@lb01 /]#cd nginx-1.6.3
[root@lb01 nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@lb01 nginx-1.6.3]#make && make install
[root@lb01 nginx-1.6.3]#ln -s /usr/local/nginx/sbin/* /usr/local/sbin
[root@lb01 nginx-1.6.3]# cd /usr/local/nginx/conf/
[root@lb01 conf]# ll nginx.conf nginx.conf.default 
-rw-r--r--. 1 root root  576 Sep 26 07:20 nginx.conf
-rw-r--r--. 1 root root 2656 Sep 26 06:33 nginx.conf.default
[root@lb01 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
[root@lb01 conf]#vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream web_pools {
    server 192.168.119.130:80 weight=5;
    server 192.168.119.133:80 weight=5;
    server 192.168.119.131:80 weight=5   backup;
}

    server {
        listen       80;
        server_name  www.test.com;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://web_pools;
        }
        }
    }

[root@lb01 conf]#cd /
[root@lb01 /]#vim /etc/hosts
192.168.119.128  www.test.com
[root@lb01 /]# nginx  #启动服务
[root@lb01 /]#

 

测试

[root@lb01 /]# curl www.test.com
web02_192.168.119.133
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web02_192.168.119.133
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web02_192.168.119.133
[root@lb01 /]# curl www.test.com
web01_192.168.119.130

 

测试backup是否正常

先关闭web01、web02的web服务

[root@web01 /]# /etc/init.d/httpd stop
Stopping httpd:                                            [  OK  ]
[root@web02 /]# /etc/init.d/httpd stop
Stopping httpd:                                            [  OK  ]

 

 测试

[root@lb01 /]# curl www.test.com
web03_backup
[root@lb01 /]# curl www.test.com
web03_backup
[root@lb01 /]# curl www.test.com
web03_backup
[root@lb01 /]# curl www.test.com
web03_backup
[root@lb01 /]# curl www.test.com
web03_backup

 

当web01和web02恢复正常时(nginx负载均衡器是否会自动切换主节点)

[root@web01 /]# /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]
[root@web01 /]# curl 192.168.119.130
web01_192.168.119.130

[root@web02 /]# /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]
[root@web02 /]# curl 192.168.119.133
web02_192.168.119.133

 

测试(可以自动切换回主节点)

[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web02_192.168.119.133
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web02_192.168.119.133
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web02_192.168.119.133
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web02_192.168.119.133
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
[root@lb01 /]# curl www.test.com
web02_192.168.119.133
[root@lb01 /]# curl www.test.com
web01_192.168.119.130
  •  upstream模块

upstream模块介绍

  Nginx的负载均衡功能依赖于ngx_http_upstream_module模块,所支持的代理方式有proxy_pass,fastcgi_pass,memcached_pass。

官方地址:http://nginx.org/en/docs/http/ngx_http_upstream_module.html

The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass directives.

  • upstream模块相关说明

upstream模块应放于nginx.conf配置的http{}标签内。

upstream模块默认算法是wrr(权重轮询weighted round-robin)

upstream模块内部参数说明

server  负载均衡后面的RS配置,可以是IP或域名,端口不写,默认是80端口。高并发场景要换成域名,通过DNS做负载均衡。

weight  是权重,默认是1.权重大接受的请求越多

max_fails=2  最大尝试失败的次数,默认为1,0表示禁止失败尝试。企业场景:建议2-3、京东1次,蓝汛10次,根据业务需求去配置。

backup  热备配置(RS节点的高可用),当前面激活的RS都失败后会自动启用热备RS。

fail_timeout=20s  失败超时时间,默认是10s。京东1次,蓝汛10次,根据业务需求去配置。常规业务2-3秒。

down  这标志着服务器永远不可用,这个参数一直配合ip_hash使用

 

Nginx负载均衡