当前位置: 移动技术网 > 网络运营>服务器>nginx > Nginx负载均衡的4种方案配置实例

Nginx负载均衡的4种方案配置实例

2019年04月21日  | 移动技术网网络运营  | 我要评论
1、轮询 轮询即round robin,根据nginx配置文件中的顺序,依次把客户端的web请求分发到不同的后端服务器。 配置的例子如下: http{

1、轮询

轮询即round robin,根据nginx配置文件中的顺序,依次把客户端的web请求分发到不同的后端服务器。
配置的例子如下:

http{ 
 upstream sampleapp { 
   server <<dns entry or ip address(optional with port)>>; 
   server <<another dns entry or ip address(optional with port)>>; 
 } 
 .... 
 server{ 
   listen 80; 
   ... 
   location / { 
    proxy_pass http://sampleapp; 
   }  
 } 

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

2、最少连接

web请求会被转发到连接数最少的服务器上。
配置的例子如下:

http{ 
  upstream sampleapp { 
    least_conn; 
    server <<dns entry or ip address(optional with port)>>; 
    server <<another dns entry or ip address(optional with port)>>; 
  } 
  .... 
  server{ 
    listen 80; 
    ... 
    location / { 
     proxy_pass http://sampleapp; 
    }  
  } 

上面的例子只是在upstream节添加了least_conn配置。其它的配置同轮询配置。

3、ip地址哈希

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

http{ 
  upstream sampleapp { 
    ip_hash; 
    server <<dns entry or ip address(optional with port)>>; 
    server <<another dns entry or ip address(optional with port)>>; 
  } 
  .... 
  server{ 
    listen 80; 
    ... 
    location / { 
     proxy_pass http://sampleapp; 
    }  
  } 

上面的例子只是在upstream节添加了ip_hash配置。其它的配置同轮询配置。

4、基于权重的负载均衡

基于权重的负载均衡即weighted load balancing,这种方式下,我们可以配置nginx把请求更多地分发到高配置的后端服务器上,把相对较少的请求分发到低配服务器。
配置的例子如下:

http{ 
  upstream sampleapp { 
    server <<dns entry or ip address(optional with port)>> weight=2; 
    server <<another dns entry or ip address(optional with port)>>; 
  } 
  .... 
  server{ 
    listen 80; 
    ... 
    location / { 
     proxy_pass http://sampleapp; 
    } 
 } 

上面的例子在服务器地址和端口后weight=2的配置,这意味着,每接收到3个请求,前2个请求会被分发到第一个服务器,第3个请求会分发到第二个服务器,其它的配置同轮询配置。

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网