1.轮询算法
1.1概述
决定负载均衡如何把请求分发给后端节点,这种分发的方式就是轮询算法.
1.2轮询算法
rr,wrr,ip_hash,lc算法(最小连接数),wlc(加权最小连接数)
负载 | 说明 |
---|
rr轮询 | round robin 轮询,默认的循环访问. |
wrr | 加权轮询,在轮询的基础上增加权重功能. server中 weight就是加权轮询 |
ip_hash | ip哈希, 只要客户端ip一样,就会一直访问同一个后端节点.(用户请求与web服务器绑定.)解决会话保持/会话共享.可能导致负载不均 |
xxx_hash | url_hash 只要用户访问的url相同/uri相同,就访问相同的web服务器.缓存服务器: 静态资源缓存. |
least_conn; | 最小连接数,lc算法. 也可以配合上权重 weight, wlc权重的最小连接数 |
一致性hash算法 | 自己研究. |
1.2.1ip_hash
upstream lb_pools {
ip_hash;
server 10.0.0.7:80 weight=1 max_fails=3
fail_timeout=30s;
server 10.0.0.8:80 weight=1 max_fails=3
fail_timeout=30s;
}
server {
listen 80;
server_name lb.oldboylinux.cn;
error_log /var/log/nginx/lb-error.log notice;
access_log /var/log/nginx/lb-access.log main;
location / {
proxy_pass http:Վˌlb_pools;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
}}
1.2.2url_hash
hash $request_uri;
2.对负载均衡进行状态检查
#由于考虑到网的问题,我们可能无法通过github来进行下载 检查状态模块,那就需要下载腾讯的tengine (根据nginx二次开发)
#下载
#解压
进入到解压后的目录中
#安装依赖
yum install -y pcre-devel openssl-devel
#配置的步骤
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock Վʔhttp-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --
user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --
with-http_auth_request_module --with-http_dav_module
--with-http_flv_module --with-http_gunzip_module --
with-http_gzip_static_module --with-http_mp4_module
--with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --
with-http_slice_module --with-http_ssl_module --
with-http_stub_status_module --with-http_sub_module
--with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --
with-stream_ssl_preread_module --with-cc-opt='-O2 -g
-pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -
fstack-protector-strong Վʔparam=ssp-buffer-size=4 -
grecord-gcc-switches -m64 -mtune=generic -fPIC' --
with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=modules/ngx_http_upstream_check_module --
add-module=modules/ngx_http_upstream_session_sticky_modu
le/
#进行编译
make -j 1
#最后的安装(略)
不需要执行make install
我们不需要在当前主机安装tengine
检查编译后生成的命令即可
./objs/nginx -V
tengine服务把ngx常用的第3方模块放在了源代码中的modules目录下面.
--add-module=modules/ngx_http_upstream_check_module 编译安装的时候增加upstream_check模块
--add-module=modules/ngx_http_upstream_session_sticky_module/ 增加会话共享模块
2.1配置文件参考
upstream lb_pools {
server 10.0.0.7:80 weight=1 max_fails=3
fail_timeout=30s;
server 10.0.0.8:80 weight=1 max_fails=3
fail_timeout=30s;
check interval=3000 rise=2 fall=5 timeout=1000
type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name lb.oldboylinux.cn;
error_log /var/log/nginx/lb-error.log notice;
access_log /var/log/nginx/lb-access.log main;
location / {
proxy_pass http:Վˌlb_pools;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
}
location /lb_status {
check_status;
access_log off;
allow 10.0.0.1;
allow 10.0.0.0/24;
deny all;
}}
upstream_check模块指令说明 | |
---|
check | 指定检查频率,失败几次,成功几次,检查间隔,检查方式 |
check_http_send | 通过http方式发出请求报文,请求报文起始行,请求方法,请求的****URI,请求协议(默认使用的是ip方式访问.) |
check_http_expect_alive | 收到指定的状态码,就认为是存活的 |
check_status; | 开启负载均衡状态检查功能,web页面.location 使用 如果加强安全 |
3.nginx-rewrite功能
重定向:也叫url重定向,也叫url改写.
未来需求:
网站是http(80)ՎՎʖhttps(443) URL重定向
用户http:Վˌwww.baidu.com ՎՎʖ https:Վˌwww.baidu.com/
3.1模块与指令
相关的指令 | 说明 |
---|
return | 实现对url的改写,一般与ngx变量一起使用. 返回指定的状态码 |
rewrite | 实现对url的改写, 使用正则匹配uri,进行改写. 还有各种标记. |
set | 创建或修改ngx变量. |
if | 判断,一般与ngx变量一起使用 |
3.1.1return指令
格式 | 说明 |
---|
格式1 | return code URL;返回状态码+新的url地址. |
格式2 | return code; 返回指定状态码. |
放哪 | server , location , if |
#####具体案例
server {
listen 80;
server_name rewrite.xujie.cn;
return 301 http:Վˌwww.baidu.com;
}
3.1.2if判断
if用于进行判断,通过ngx中变量.
可以比大小.
也可以进行等于,不等于.
也可以进行匹配(过滤).
if指令 | |
---|
格式 | |
放在哪里 | server , location |
可以使用的符号 | |
if指令在ngx中的格式
if (条件) {
满足条件执行的内容.
}
使用到的变量: $request_method 取出请求方法.
##配置文件参考
server {
listen 80;
server_name rewrite.oldboylinux.cn;
root /app/code/rewrite ;
if ( $request_method !~ "GET|POST" ) {
return 403;
}
location / {
index index.html;
}
}
3.1.3set
用于自己创建或修改ngx变量.
#shell写法
oldboy=666
echo $oldboy
#ngx中写法
set $变量 值;
set $lidao 996;
##参考配置文件
server {
listen 80;
server_name rewrite.xujie.cn;
set $oldboy "xujie996";
return 200 $oldboy;
}
3.1.4rewrite
rewrite正则用于匹配用户请求的uri.
命令的格式与sed 'sՎՎ˂g' 类似,实现替换功能,rewrite替换url内容.(改写)
rewrite指令 | 说明 |
---|
格式 | rewrite 找什么(具体内容/正则/保护分组) 替换成什么(具体内容,后向引用) [标记]; 标记可以省略,默认使用 redirect标记(302) |
放在哪里 | server , location , if |
| rewrite匹配的内容,匹配uri. |
rewrite的301,302标记 | |
redirect | 302 |
permanent | 301 |
##参考配置
server {
listen 80;
server_name rewrite.oldboylinux.cn;
#return 301 http://www.baidu.com$request_uri;
#http://ˌ rewrite.oldboylinux.cn/images/lidao.txt
#http://rewrite.oldboylinux.cn
rewrite ^(.*)$ http:Վˌwww.baidu.com$1 ;
}