3.1 A/B testing
upstream httplb66 {
#ip_hash;
server 192.168.11.66:80;
}
upstream httplb69 {
server 192.168.11.69:80;
}
split_clients "${request_method}AAA" $nodes {
20% httplb66;
* httplb69;
}
server {
listen 80;
server_name www.example.com;
root /www;
location = / {
}
#http load balance
location ^~ /httplb/ {
index index.html;
proxy_pass http://$nodes/;
}
}
测试中GET方法被代理到66,HEAD被代理到69,对给定变量进行hash计算后,下面的20%等又怎么算?
3.2 使用GeoIP模块和数据库
1.安装
yum install nginx-module-geoip
2.Download the 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
3.启用
load_module modules/ngx_http_geoip_module.so;
load_module modules/ngx_stream_geoip_module.so;
http {
geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
...
}
3.5 Limiting Connections
http {
limit_conn_zone $binary_remote_addr zone=limitbyaddr:10m;
limit_conn_status 429;
server {
limit_conn limitbyaddr 40;
}
}
不知道怎么测试连接,用curl --no-keepalive没用
3.6 Limiting Rate
http {
limit_req_zone $binary_remote_addr zone=limitbyaddr:10m rate=1r/s;
limit_req_status 429;
limit_req_log_level error;
server {
limit_req zone=limitbyaddr burst=10 nodelay;
}
}
用ab测试,或者用for循环测试时,rate超过时会返回429;使用 r e m o t e a d d r 作 为 k e y 时 也 可 以 , 根 据 I P 地 址 的 全 长 来 计 算 ; 使 用 remote_addr作为key时也可以,根据IP地址的全长来计算;使用 remoteaddr作为key时也可以,根据IP地址的全长来计算;使用request_method作为key时,使用以下脚本测试,可以区别GET和HEAD方法:
#!/bin/bash
count=0
for i in {1..5} ; do
for i in {1..50} ; do
curl http://www.example.com/
done
curl http://www.example.com/ -I
done
使用for循环测试,不同客户端使用GET方法,超过rate时会返回429
3.7 Limiting Bandwidth
location /download/ {
autoindex on;
autoindex_format html;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate_after 50m;
limit_rate 100k;
}