nginx负载均衡一模块介绍

一、核心模块upstream

1.upstream模块介绍

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

2.相关参数

  • a)upstream模块应放与nginx.conf配置的http{}标签内.
  • b)upstream模块默认算法是wrr(权重轮询)

2.1内部参数

weight:权重,默认是1,权重越大接受的请求越多
max_fail=2:最大尝试失败次数,默认为1,0表示禁止失败尝试。
backup:热备配置
fail_timeout=20s:失败超时时间,默认是10s,比如服务器宕机,检查三次都没起来,会停20秒钟在继续检查
down:标志着的服务器永远不可用,配合ip_hash用。

3.调度算法

3.1rr轮询(默认)

  • 按客户端请求顺序把客户端的请求逐一分配到不同的后端的服务器,这相当于lvs中 rr算法,如果后端服务器宕机(默认值检测80端口),当即服务器会被自定剔除,使用户访问不受影响,请求会分配给正常的服务器。

3.2weight(权重)

  • 在轮询算法的基础上加上权重(默认是rr+weight),权重轮询和访问成正比,权重越大 转发的请求也就越多

3.3ip_hash

  • 每个请求按访问的IP的hash结果分配,当新的请求到达时,先将其客户端通过哈希算法希出一个值,在随后请求客户端,ip的哈希值只要相同,就会被分值至同一台服务器,该调度 算法可以解决网页session共享问题,但有时会导致请求分配不均,及无法保证1:1的负载均衡。

3.4fair动态算法

  • 按照后端服务器节点的相应时间来分配请求,响应时间短的优先分配
    比上面两个更加智能的算法,这种算法可以依据页面大小和加载时间长短智能的进行负载均衡
  • nginx本身不支持fair,需要下载nginx的upstream_fair模块。

3.5url_hash

  • 按访问URL的hash结果来分配请求,让每个URL定向到同一个后端服务器,后端服务器为 缓存服务器时效果显著,

二、http proxy 模块

1.http proxy介绍

  • nginx的代理功能是通过http proxy 模块来实现的,默认在安装nginx时已经安装了http proxy_pass模块,因此可以直接使用。

2.参数

官方帮助文档:http://nginx.org/en/docs/http/ngx_http_proxy_module.html

prox_pass http://ip:用于指定方向代理的服务器池
proxy_set_header:设置由后端的服务器获取用户的主机名或者真是IP地址,以及代理者的真实IP地址
    ===>proxy_set_header Host $host:当后端web服务器上也配置有多个虚拟主机时,需要用该header来区分反向代理哪个主机名
    ===>proxy_set_header X-Forwarded-For $remote_addr:如果后端web服务器上的程序需要获取用户IP,从该header获取
client_boby_buffer_size:用于指定客户端请求主体缓冲区大小,可以理解为先保存到本地再传给用户
proxy_connect_timeout :表示与后端服务器连接的超时时间,及发起握手等候相应的超时时间
proxy_send_timeout:表示后端服务器的数据回传时间,即在规定时间之内后端服务器必须传完所有数据,否则将断开连接
proxy_read_timeout:设置nginx从代理的后端服务器获取信息的时间,表示连接建立成功后,nginx等待后端服务器的响应时间
proxy_buffer_size:设置缓冲区大小,默认 该缓冲区大小等于指令等于proxy_buffers的大小
proxy_buffers:设置缓冲区的数量和大小,nginx从代理的后端服务器获取的响应信息,会放置到换缓冲区
proxy_busy_buffers_size:用于设置系统很忙时可以使用的proxy_buffers大小
pro_temp_file_write_size:指定proxy缓存临时文件的大小

3.配置

[root@lb01 conf]# cat proxy.conf 
proxy_redirect off; 
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_connect_timeout 90; 
        proxy_send_timeout 90; 
        proxy_read_timeout 90; 
        proxy_buffer_size 4k; 
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k; 
        proxy_temp_file_write_size 64k;
[root@lb01 conf]# vi nginx.conf
 19         location / {
 20             root   html;
 21             index  index.html index.htm;
 22             proxy_pass http://lbproxy;
 23             include proxy.conf;###添加
 24         }
 [root@lb01 conf]# nginx -t
nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful
[root@lb01 conf]# nginx -s reload

4.部署两个虚拟主机

[root@lb01 test]# tree html/###两台虚拟主机首页文件
html/
├── bbs
│   └── index.html
└── www
    └── index.html

2 directories, 2 files

[root@lb01 test]# ls httpd.conf ###提前配置两台虚拟主机
httpd.conf
[root@lb01 test]# cat httpd.yml ###ansible-playbook剧本
---
- hosts: client
  tasks:
    - name: cp index
      copy: src=/test/html/ dest=/var/www/html/
    - name: cp conf
      copy: src=/test/httpd.conf dest=/etc/httpd/conf/
    - name: restart httpd
      service: name=httpd state=restarted
[root@lb01 test]# ansible-playbook httpd.yml

5.测试

[root@lb01 test]# echo "10.0.0.10 www.liang.com bbs.liang.com" >>/etc/hosts
[root@lb01 test]# curl www.liang.com
www
[root@lb01 test]# curl bbs.liang.com
bbs

猜你喜欢

转载自blog.csdn.net/liang_operations/article/details/81628812