码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx-负载均衡

时间:2018-12-04 00:56:41      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:sed   orm   测试的   for   源地址   二次   stat   最大   核心   

Nginx负载均衡:

Nginx核心功能之一可以当做负载均衡器使用!
ngx_http_upstream_module模块 

基本用法:
upstream name { 
    server1 
    server2
} 
官网说明:
Syntax: upstream name { ... }
Default:    —
Context:http

location ~* \.(jsp|do)$ {
    proxy_pass http://name;
}

nginx主机:192.168.23.48
后端主机:
RS1:192.168.23.36
RS2:192.168.23.37

具体实现:
http语句块中设置:
upstream apache {
    server 192.168.23.36:80;
    server 192.168.23.37:80;
}

虚拟主机调用:
server {
    server_name www.a.com;
    listen 80;
    root /web/a.com;
    index index.html;
    server_tokens off;
    access_log /web/a.com/a.com.log test;
    location / {
        proxy_pass http://apache;
    }
}

测试结果:
[root@www21:45:46nginx]#for i in {1..1000};do curl www.a.com ; sleep 1;done
<h1>test RS1 server</h1>
<h1>test RS2 server</h1>

属性设置:  
weight=number  权重,默认为1  
max_conns    连接后端报务器最大并发活动连接数,1.11.5后支持  
max_fails=number 失败尝试最大次数;超出此处指定的次数时
server将被标 记为不可用,默认为1  
fail_timeout=time  后端服务器标记为不可用状态的连接超时时长,默认10s  
backup 将服务器标记为"备用",即所有服务器均不可用时才启用  
down  标记为"不可用",实现灰度发布 

测试属性使用:
upstream apache {
    server 192.168.23.36:80 down;
    server 192.168.23.37:80 max_conns=1000;
    server 127.0.0.1:80 backup;
}

测试结果:
1 198.168.23.36的页面不会出现:
[root@www21:51:31nginx]#for i in {1..1000};do curl www.a.com ; sleep 1;done
<h1>test RS2 server</h1>
<h1>test RS2 server</h1>
<h1>test RS2 server</h1>
2 所有主机都宕机之后,自动自用backup主机:
3 自带健康性检查,后端主机重新上线会立刻停止使用
backup
4 当并发量超过1000时,访问会失败!
root@www21:54:56nginx]#ab -c 1001 -n 10000 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.a.com (be patient)
Completed 1000 requests
apr_socket_recv: Connection reset by peer (104)
Total of 1843 requests completed

调度算法:
wrr 加权重的轮询
ip_hash 源地址hash调度方法
least_conn 最少连接调度算法
hash key [consistent] 基于指定的key的hash表来实现对请求的调度
作用:将请求分类,同一类请求将发往同一个upstream server
使用 consistent参数将使用ketama一致性hash算法适用于后端是Cache服务器 
[如varnish]时使用  
hash $request_uri consistent;  
一致性哈希算法:
hash(ip1)/2^32取模
会有哈希环偏移问题

需要使用权重尽量避免[权重要尽量调大]
权重大hash(ip1+[权重个]随机数)

hash $remote_addr;

keepalive 连接数N
为每个worker进程保留的空闲的长连接数量
可节约nginx端口,并减少连接管理的消耗 

测试一致性哈希算法:
配置:
hash $request_uri consistent;
测试:
[root@www22:00:26nginx]#for i in {1..1000};do curl www.a.com/test1.html ; sleep 1;done
Test RS1 page1
Test RS1 page1
^C
[root@www22:00:45nginx]#for i in {1..1000};do curl www.a.com/test2.html ; sleep 1;done
Test RS1 page2
Test RS1 page2
^C
[root@www22:01:04nginx]#for i in {1..1000};do curl www.a.com/test3.html ; sleep 1;done
Test RS1 page3
Test RS1 page3
^C
[root@www22:01:09nginx]#for i in {1..1000};do curl www.a.com/test4.html ; sleep 1;done
Test RS1 page4
Test RS1 page4
^C
[root@www22:01:14nginx]#for i in {1..1000};do curl www.a.com/test5.html ; sleep 1;done
Test RS2 page5
Test RS2 page5
结果证明使用同一个url访问会调度到同一个后端主机

相关企业版参数:

health_check [parameters];  
健康状态检测机制;只能用于location上下文[企业版专用]  

常用参数:  
interval=time检测的频率,默认为5秒  
fails=number:判定服务器不可用的失败检测次数;默认为1次  
passes=number:判定服务器可用的失败检测次数;默认为1次  
uri=uri:做健康状态检测测试的目标uri;默认为/  
match=NAME:健康状态检测的结果评估调用此处指定的match配置块 

match name { 
    ... 
}  
对backend server做健康状态检测时,定义其结果判断机制;

只能用于http 上下文 ?

常用的参数:[企业版专用]  
status  code[  code ...]: 期望的响应状态码  
header  HEADER[operator  value]:期望存在响应首部,
也可对期望的响 应首部的值基于比较操作符和值进行比较  
body:期望响应报文的主体部分应该有的内容 

Nginx模拟4层调度:

ngx_stream_core_module模块 
模拟反代基于tcp或udp的服务连接,即工作于传输层的反代或调度器
官网说明:
Syntax: stream { ... }
Default:—
Context:main

具体使用:
stream {  
    upstream mysqlsrvs {   
        server 192.168.23.36:3306;    
        server 192.168.23.37:3306;    
    }  
    server {   
        listen 3306;   
        proxy_pass mysqlsrvs;  
    } 

}

测试结果:
第一次访问:
[root@www22:27:01nginx]#mysql -uyl -p[密码] -h 172.20.23.48
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linux2             |--区别看这里
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

第二次访问:
[root@www22:28:17nginx]#mysql -uyl -p[密码] -h 172.20.23.48
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linux1             |--区别看这里
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

Nginx-负载均衡

标签:sed   orm   测试的   for   源地址   二次   stat   最大   核心   

原文地址:http://blog.51cto.com/13878078/2325588

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!