레이블이 traffic인 게시물을 표시합니다. 모든 게시물 표시
레이블이 traffic인 게시물을 표시합니다. 모든 게시물 표시

20140817

nginx 국가별 대역폭 (using geoip)

Nginx 에서 GeoIP 를 이용해 국가별로 접속 대역폭을 설정할수 있습니다.
Maxmind 에서 제공하는 GeoLite 국가  데이터베이스 CSV 를 다운로드 합니다.

# wget  http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
# unzip GeoIPCountryCSV.zip
Archive:  GeoIPCountryCSV.zip
inflating:  GeoIPCountryWhois.csv

CSV 내용을 국가별 아이피 대역, 국가명으로 간단하게 Perl Script 로 분리 하여 따로 저장  합니다.

변경 전 :  "1.11.0.0","1.11.255.255","17498112","17563647","KR","Korea, Republic of"
변경  후 : 1.11.0.0/16 KR;

# wget  http://mirror.yongbok.net/ruo91/nginx/script/geo2nginx.pl
#  chmod +x geo2nginx.pl
# ./geo2nginx.pl <  GeoIPCountryWhois.csv >  /usr/local/etc/nginx/nginxGeo.txt

nginx.conf 파일의 http 부분에 추가

geo $country {
    default no;
    include  /usr/local/etc/nginx/nginxGeo.txt;
}

가상호스트 부분인 server 부분에  대역폭 설정 구문을 추가

if ($country ~ ^(?:US|CA|ES)$ ){
    set  $limit_rate 500k;
}
if ($country ~ ^(?:RU|CN)$ ){
    set $limit_rate  1k;
}


nginx 대역폭 제한

nginx.conf 수정

location ... { 

    최대 대역폭
    limit_rate 200k;

    일정 용량을 전송 제한
    limit_rate_after 원하는용량M;

}

Articles