콘텐츠로 이동

2024 07 01

2024-07-01

Nginx geo 모듈

참고: http://nginx.org/en/docs/http/ngx_http_geo_module.html

  • 개요
    • ngx_http_geo_module 클라이언트 IP 주소에 따라 변수를 생성함
  • 예시
    geo $allowed_ip {
        default        0;
        127.0.0.1      1;
        192.168.1.0/24 1;
        10.1.0.0/16    1;
        2001:0db8::/32 1;
    }
    
    location = /test {
        if ($allowed_ip) {
          rewrite ^(.*)$ /index.html break;
        }
        return 404;
    }
    
  • 설명

    • 기본적으로 $remote_addr에서 IP 주소를 가져와서 비교
    • 만약 다른 변수에서 추출하고 싶다면 하기처럼도 가능
      geo $arg_remote_addr $geo {
          ...;
      }
      
    • 변수가 알맞은 ip가 아니라면, "255.255.255.255"로 간주됨
    • 변수로는 ip, CIDR, range가 사용 가능.
    • geo가 아마 지역기반으로 쓰라고 해서 이렇게 만들어진듯.
      geo $country {
          ranges;
          default                   ZZ;
          127.0.0.0-127.0.0.0       US;
          127.0.0.1-127.0.0.1       RU;
          127.0.0.1-127.0.0.255     US;
          10.1.0.0-10.1.255.255     RU;
          192.168.1.0-192.168.1.255 UK;
      }
      

Nginx map 모듈

참고: http://nginx.org/en/docs/http/ngx_http_map_module.html

  • 개요
    • ngx_http_map_module 변수를 다른 변수로 매핑함
  • 예시
    map $http_host $name {
        hostnames;
    
        default       0;
    
        example.com   1;
        *.example.com 1;
        example.org   2;
        *.example.org 2;
        .example.net  3;
        wap.*         4;
    }
    
    map $http_user_agent $mobile {
        default       0;
        "~Opera Mini" 1;
    }
    
  • 설명
    • source -> resulting values
    • source는 String/Regex 중 하나
      • String은 case 상관없이 매칭
      • Regex는 ~로 시작하며, case-sensitive함
        • "~*"로 case-insensitive 하게 구현할 수는 있음
    • default 값을 줄 수 있음
    • source가 하나 이상의 값과 매칭이 된다면, 첫번째 꺼를 채택해서 처리
    • HashMap의 버킷사이즈는 프로세서의 캐시 라인 사이즈로 지정