Nginx负载均衡是Nginx的核心功能之一,工作在第七层。它是除了lvs,haproxy之外市面上较为流行的一种负载均衡软件。可以将客户端请求分流到跨多个计算资源(如计算机,计算机集群,网络链接,中央处理单元或磁盘驱动器)的工作负载分布。负载均衡旨在优化资源使用,最大化吞吐量,最小化响应时间,并避免任何单一资源的过载。使用具有负载平衡的多个组件而不是单个组件可以通过冗余来提高可靠性和可用性。本文简要描述Nginx负载均衡的配置,供大家参考。
一、负载均衡upstream模块介绍
upstream模块可定义一个新的上下文,它包含了一组后端upstream服务器,这些服务器可能被赋予了不同的权重、不同的类型甚至可以基于维护等原因被标记为down。 
 upstream语法及示例 
   语法:upstream name { … } 
   声明一组可以被proxy_pass和fastcgi_pass引用的服务器;这些服务器可以使用不同的端口,并且也可以使用Unix Socket;也可以为服务器指定不同的权重; 
 例如: 
 upstream backend { 
 server backend1.example.com weight=5 down backup; 
 server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; 
 server unix:/tmp/backend2; 
 }
upstream模块常用的指令有: 
 ip_hash 
   基于客户端IP地址完成请求的分发,它可以保证来自于同一个客户端的请求始终被转发至同一个upstream服务器; 
 keepalive 
   每个worker进程为发送到upstream服务器的连接所缓存的个数; 
 least_conn 
   最少连接调度算法; 
 server 
   定义一个upstream服务器的地址,还可包括一系列可选参数,如: 
     weight:权重; 
     max_fails:最大失败连接次数,失败连接的超时时长由fail_timeout指定; 
     fail_timeout:等待请求的目标服务器发送响应的时长; 
     backup:用于fallback的目的,所有服务均故障时才启动此服务器; 
     down:手动标记其不再处理任何请求;
upstream模块的负载均衡轮询算法
轮询(round-robin 默认) 
   每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 
 权重(weight) 
   指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 
 IP哈希(ip_hash) 
   每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 
 第三方(fair) 
   按后端服务器的响应时间来分配请求,响应时间短的优先分配。 
 第三方(url_hash)
二、后端服务器1配置
说明,当前的演示环境中,前后端全部使用Nginx。
查看主机名及IP
# hostname
centos7-web.example.com
# ip addr|grep inet|grep global
inet 172.24.8.128/24 brd 172.24.8.255 scope global eno16777728
Nginx版本
# Nginx -v
Nginx version: Nginx/1.9.0
添加测试文件
# mv /etc/Nginx/html/index.html /etc/Nginx/html/index.html.bk
# echo "This a test home page from 172.24.8.128">/etc/Nginx/html/index.html
# ss -nltp|grep Nginx
LISTEN 0 128 *:90 *:* users:(("Nginx",pid=2399,fd=6),("Nginx",pid=2398,fd=6))
# curl http://localhost:90
This a test home page from 172.24.8.128 
三、后端服务器2配置
查看主机名及IP
# hostname
node132
# ip addr|grep inet|grep global
inet 192.168.1.132/24 brd 192.168.1.255 scope global eth0
Nginx版本
# Nginx -v
Nginx version: Nginx/1.10.2
添加测试文件
# cp /usr/share/Nginx/html/index.html /usr/share/Nginx/html/index.html.bk
# echo "This a test home page from 192.168.1.132">/usr/share/Nginx/html/index.html
# ss -nltp|grep Nginx
LISTEN 0 128 :::80 :::* users:(("Nginx",2808,7),6992,7))
#
# curl http://localhost
This a test home page from 192.168.1.132 
四、Nginx负载均衡配置及验证
负载均衡主机名及IP
# hostname
centos7-router
# ip addr|grep inet|grep global
inet 172.24.8.254/24 brd 172.24.8.255 scope global eno16777728
inet 192.168.1.175/24 brd 192.168.1.255 scope global dynamic eno33554960
Nginx版本
# Nginx -v
Nginx version: Nginx/1.12.2
负载均衡配置
# vim /etc/Nginx/conf.d/slb.conf
upstream www {
    server 172.24.8.128:90 max_fails=3 fail_timeout=30s;
    server 192.168.1.132:80 max_fails=3 fail_timeout=30s;
    keepalive 32;
}
server {
    listen 9090;
    server_name localhost;
    location / {
        proxy_set_header Host $host;
        proxy_set_header x-for $remote_addr;
        proxy_set_header x-server $host;
        proxy_set_header x-agent $http_user_agent;
        proxy_pass http://www;
}
}
验证负载均衡效果
# systemctl reload Nginx
# curl http://localhost:9090
This a test home page from 172.24.8.128
# curl http://localhost:9090
This a test home page from 192.168.1.132
# curl http://localhost:9090
This a test home page from 172.24.8.128
# curl http://localhost:9090
This a test home page from 192.168.1.132
配置IP hash轮询策略,修改后的部分内容如下
# head -n6 /etc/Nginx/conf.d/slb.conf
upstream www {
    ip_hash;
    server 172.24.8.128:90 max_fails=3 fail_timeout=30s;
    server 192.168.1.132:80 max_fails=3 fail_timeout=30s;
    keepalive 32;
    ....
}
# systemctl reload Nginx
# curl http://localhost:9090
This a test home page from 172.24.8.128
# curl http://localhost:9090
This a test home page from 172.24.8.128
# curl http://localhost:9090
This a test home page from 172.24.8.128 
经测试ip_hash轮询还是存在一定的问题,就是不论在那台机器访问,始终请求到第一台机器。 
 之前也做过一次测试没有成功。生产环境后来换成了Tengine。 version: Tengine/2.1.2 (Nginx/1.6.2) 
 在Tengine中使用session_sticky指令实现session粘滞。 
 关于这个ip_hash,有网友描述了这个原因:不论A类B类C类等网络地址,Nginx的ip_hash算法都将一个ip地址的前三段作为hash的关键字。 
Nginx的ip_hash指令 http://blog.csdn.net/fygkchina/article/details/41841915
五、基于Nginx代理到tomcat的配置(演示略)
# more tomcat.conf 
upstream app {
                ip_hash;
                server 192.168.81.146:8080;
                server 192.168.81.147:8080;
                keepalive 32;
}
server {
    listen 80;
    server_name localhost;
    location / {
        proxy_pass http://app;
        proxy_set_header Host $http_host;   #Auhtor : Leshami
        proxy_set_header X-Real-IP $remote_addr;  #Blog : http://blog.csdn.net/leshami
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-scheme $scheme;
        proxy_set_header x-agent $http_user_agent;
    }
server {
    listen      443 ssl;
    server_name  localhost;
    server_name node132.ydq.com;
    ssl_certificate      /etc/Nginx/conf.d/node132.ydq.com.crt;
    ssl_certificate_key  /etc/Nginx/conf.d/node132.ydq.com.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    location / {
        proxy_pass http://app;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-Port $remote_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-scheme $scheme;
        proxy_set_header x-agent $http_user_agent;
        add_header backendIP $upstream_addr;
        proxy_set_header Proxy_Port $proxy_port;
    }
}