Nginx系列(二)——流量分发管控

A/B Testing
金丝雀
同时提供多个版本的文件或应用服务,没有问题的话后续可以全部更新
使用split_clients模块对流量进行分发,20%发给ip1,80%发给ip2
split_clients类似upstream功效
split_clients "${remote_addr}AAA" $variant {
    20.0% "ip1";
    * "ip2";
}

location / {
    proxy_pass http://$variant
}


GeoIP Module and Database
根据区域划分流量?
需要额外安装GeoIp数据库,并将其嵌入到Nginx
yum install nginx-module-geoip -y
下载GeoIP country and city databases and unzip them:
# mkdir /etc/nginx/geoip
# cd /etc/nginx/geoip
# wget "http://geolite.maxmind.com/\
download/geoip/database/GeoLiteCountry/GeoIP.dat.gz"
# gunzip GeoIP.dat.gz
# wget "http://geolite.maxmind.com/\
download/geoip/database/GeoLiteCity.dat.gz"
# gunzip GeoLiteCity.dat.gz
使用
load_module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so";
http {
geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
...
}

Restricting Access Based on Country
禁止指定国家访问
load_module
"/usr/lib64/nginx/modules/ngx_http_geoip_module.so";
http {
    map $geoip_country_code $country_access {        #设置$country_access对应的值为1或0
        "US" 0;    #ip来自美国,设置对应值为0
        "RU" 0;
        default 1;    #默认值为1
    }
    ...
}
server {
    if ($country_access = '1') {    #值为默认值时直接拒绝
        return 403;
    }
    ...
}

Finding the Original Client
在Nginx前还有其他代理。可以通过Nginx的 X-Forwarded-For header进行递归查找分析源IP
load_module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so";
http {
geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
geoip_proxy 10.0.16.0/26;
geoip_proxy_recursive on;
...
}

Limiting Connections
限制连接。构建一个共享内存区域,限制打开的连接数
http {
limit_conn_zone $binary_remote_addr zone=limitbyaddr:10m;
limit_conn_status 429;    #返回过多请求数代码429
...
server {
...
limit_conn limitbyaddr 40;
...
}
}

Limiting Rate
使用rate-limiting模块进行限速。可用于防止拒绝式攻击
http {
limit_req_zone $binary_remote_addr
zone=limitbyaddr:10m rate=1r/s;
limit_req_status 429;
...
server {
...
limit_req zone=limitbyaddr burst=10 nodelay; #达到10后才开始延迟
...
}
}

Limiting Bandwidth
限制客户端下载带宽。
location /download/ {
limit_rate_after 10m;    #大部分数据传输完成后,才允许超过10M?
limit_rate 1m;
}

猜你喜欢

转载自www.cnblogs.com/biaopei/p/12925284.html