Nginx的负载均衡方案详解

作者:chszs,转载需注明。博客主页:http://blog.csdn.net/chszs

Nginx的负载均衡方案有:

1、轮询

轮询即Round Robin,根据Nginx配置文件中的顺序,依次把客户端的Web请求分发到不同的后端服务器。

配置的例子如下:

[html]  view plain copy print ?
  1. http{  
  2.   upstream sampleapp {  
  3.       server <<dns entry or IP Address(optional with port)>>;  
  4.       server <<another dns entry or IP Address(optional with port)>>;  
  5.   }  
  6.   ....  
  7.   server{  
  8.      listen 80;  
  9.      ...  
  10.      location / {  
  11.         proxy_pass http://sampleapp;  
  12.      }    
  13.   }  

上面只有1个DNS入口被插入到upstream节,即sampleapp,同样也在后面的proxy_pass节重新提到。

标准的代理配置

下面是一个基本的反向代理配置模板,将所有请求都转发给指定的后端应用。

例如,到http://your.ip:80/的请求都将重定向到 http://127.0.0.1:4433/ 私有服务器:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# One process for each CPU-Core
worker_processes  2;
# Event handler.
events {
     worker_connections  8096;
     multi_accept        on;
     use                 epoll;
}
http {
      # Basic reverse proxy server
      upstream backend  {
            server 127.0.0.1:4433;
      }
      # *:80 -> 127.0.0.1:4433
      server {
             listen       80;
             server_name  example.com;
             ## send all traffic to the back-end
             location / {
                  proxy_pass        http: //backend ;
                  proxy_redirect    off;
                  proxy_set_header  X-Forwarded-For $remote_addr;
             }
      }
}


2、最少连接

Web请求会被转发到连接数最少的服务器上。

配置的例子如下:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. http{  
  2.     upstream sampleapp {  
  3.         least_conn;  
  4.         server <<dns entry or IP Address(optional with port)>>;  
  5.         server <<another dns entry or IP Address(optional with port)>>;  
  6.     }  
  7.     ....  
  8.     server{  
  9.        listen 80;  
  10.        ...  
  11.        location / {  
  12.           proxy_pass http://sampleapp;  
  13.        }    
  14.     }  
上面的例子只是在upstream节添加了least_conn配置。其它的配置同轮询配置。

3、IP地址哈希

前述的两种负载均衡方案中,同一客户端连续的Web请求可能会被分发到不同的后端服务器进行处理,因此如果涉及到会话Session,那么会话会比较复杂。常见的是基于数据库的会话持久化。要克服上面的难题,可以使用基于IP地址哈希的负载均衡方案。这样的话,同一客户端连续的Web请求都会被分发到同一服务器进行处理。

配置的例子如下:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. http{  
  2.     upstream sampleapp {  
  3.         ip_hash;  
  4.         server <<dns entry or IP Address(optional with port)>>;  
  5.         server <<another dns entry or IP Address(optional with port)>>;  
  6.     }  
  7.     ....  
  8.     server{  
  9.        listen 80;  
  10.        ...  
  11.        location / {  
  12.           proxy_pass http://sampleapp;  
  13.        }    
  14.     }  
上面的例子只是在upstream节添加了ip_hash配置。其它的配置同轮询配置。

4、基于权重的负载均衡

基于权重的负载均衡即Weighted Load Balancing,这种方式下,我们可以配置Nginx把请求更多地分发到高配置的后端服务器上,把相对较少的请求分发到低配服务器。

配置的例子如下:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. http{  
  2.     upstream sampleapp {  
  3.         server <<dns entry or IP Address(optional with port)>> weight=2;  
  4.         server <<another dns entry or IP Address(optional with port)>>;  
  5.     }  
  6.     ....  
  7.     server{  
  8.        listen 80;  
  9.        ...  
  10.        location / {  
  11.           proxy_pass http://sampleapp;  
  12.        }  
  13.   }  
上面的例子在服务器地址和端口后weight=2的配置,这意味着,每接收到3个请求,前2个请求会被分发到第一个服务器,第3个请求会分发到第二个服务器,其它的配置同轮询配置。

还要说明一点,基于权重的负载均衡和基于IP地址哈希的负载均衡可以组合在一起使用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值