[깊이] nginx geoip모듈 모듈 (26)

8222 단어
nginx 튜 토리 얼 세트 가 져 오기, 왈 리 블 로 그 를 방문 하 십시오.
ngx_http_geoip_module 은 IP 주소 에 대한 지역 정 보 를 읽 습 니 다.클 라 이언 트 가 방문 할 때 ip 주 소 를 통 해 클 라 이언 트 가 있 는 국가, 도 시 를 알 수 있 습 니 다.서로 다른 나라 의 고객 방문 을 처리 하 는 데 자주 쓰 인 다.
앞서 반찬 은 yum 소스 로 nginx 전송 문과 nginx 부 드 러 운 업그레이드 전송 문 을 설치 했다 고 적 혀 있 습 니 다.nginx 부 드 러 운 업그레이드 에서 원본 코드 로 모듈 을 추가 하 는 것 을 소개 합 니 다.
  • [email protected]블 로그 {: target = " blank"}
  • 컴 파일 할 때 오류 가 발생 했 습 니 다
  • 1. nginx - module - geoip 다운로드
    세 번 째 소절 에 nginx 설 치 를 기록 할 때 기본적으로 이 모듈 이 설치 되 어 있 지 않 습 니 다.수 동 다운로드 ngx_http_geoip_module 모듈 이 필요 합 니 다.
    yum install nginx-module-geoip
    

    설치 에 성공 하면 /etc/nginx/module 폴 더 에 geoip 모듈 이 나타 납 니 다.
    2. MaxMind 의 GeoIP 라 이브 러 리 설치
    GeoIP 라 이브 러 리 다운로드 디 렉 터 리 /opt/download 폴 더 아래
    cd /opt/download  #        
    wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
    tar -zxvf GeoIP.tar.gz
    cd GeoIP-1.4.8
    ./configure
    make
    make install
    

    방금 설 치 된 라 이브 러 리 는 / usr / local / lib 에 자동 으로 설치 되 어 있 기 때문에 이 디 렉 터 리 는 관련 프로그램 을 실행 할 때 이 GeoIP 라 이브 러 리 에 자동 으로 연결 할 수 있 도록 동적 링크 설정 에 추가 해 야 합 니 다.
    echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
    ldconfig
    

    3. IP 데이터베이스 다운로드
    최종 디 렉 터 리 구조
    /etc/nginx/data/geoip
    |-GeoIP.dat
    |-GeoLiteCity.dat
    

    MaxMind 는 무료 IP 지역 데이터 베 이 스 를 제공 합 니 다. 이 데이터 베 이 스 는 바 이 너 리 입 니 다. 텍스트 편집기 로 열 수 없습니다. 위의 GeoIP 라 이브 러 리 에서 읽 어야 합 니 다.반찬 은 여기에 공식 다운로드 주 소 를 놓 고 공식 다운로드 {: target = "Blank"} 파일 과 GeoIP.dat.gz 파일 을 다운로드 해 야 합 니 다.반찬 은 여기 클 라 우 드 디스크 를 놓 아서 국내 동료 들 이 클 라 우 드 디스크 주 소 를 쉽게 얻 을 수 있 도록 합 니 다 {: target = " blank"} 추출 코드 [5ft 5]
    cd /etc/nginx/data/geoip  #    data,      
    
    gunzip GeoIP.dat.gz  #  
    gunzip GeoLiteCity.dat.gz  #  
    

    4. 컴 파일 설치
    기본 작업 준비 완료 후 우 리 는 ngxhttp_geoip_module 컴 파일 nginx.ngx_http_geoip_module 문서 {: target = " blank"} 기본적으로 이 모듈 을 구축 하지 않 습 니 다. GeoLiteCity.dat.gz 설정 파 라미 터 를 사용 해 야 합 니 다.
    이전에 nginx 부 드 러 운 업그레이드 에서 반찬 컴 파일 nginx 의 원본 패 키 지 는 --with-http_geoip_module 폴 더 아래 에 두 었 습 니 다.
    cd /opt/download/nginx-1.14.2
    

    컴 파일 매개 변수 가 져 오기
    nginx -V
    

    출력
    --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'
    컴 파일 매개 변수 추가 /opt/download/nginx-1.14.2--with-http_geoip_module
    컴 파일
    ./configure         
    

    설치 하 다.
    make && make install
    

    컴 파일 에 오류 가 있 으 면 맨 위 --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' --with-http_geoip_module 를 보십시오.친구 들 한테 도움 이 됐 으 면 좋 겠 어 요.
    검출
    nginx -V
    

    출력 컴 파일 매개 변수 가 있 는 지 확인 하 십시오 . 있 으 면 설 치 된 것 을 증명 합 니 다.
    5. 구성 모듈
    geoip_country
    Syntax: geoip_country file;
    Default: —
    Context: http
    

    매개 변수 이름
    묘사 하 다.
    $geoip_country_code
    두 자모의 국가 / 지역 코드, 예 를 들 어 "RU", "US"
    $geoip_country_code3
    세 자모의 국가 / 지역 코드, 예 를 들 어 "RUS", "USA"
    $geoip_country_name
    국명
    geoip_city
    Syntax: geoip_city file;
    Default: —
    Context: http
    

    매개 변수 이름
    묘사 하 다.
    $geoip_area_code
    전화 지역 번호 (미국 만 가능)
    $geoip_city_continent_code
    두 자모의 대륙 코드, 예 를 들 면 "EU", "NA"
    $geoip_city_country_code
    두 자모의 국가 / 지역 코드, 예 를 들 어 "RU", "US"
    $geoip_city_country_code3
    세 자모의 국가 / 지역 코드, 예 를 들 어 "RUS", "USA"
    $geoip_city_country_name
    국명
    $geoip_dma_code
    Google 애 드 워 즈 API 의 지리 적 위치 에 따라 미국의 DMA 지역 코드 (도시 코드 라 고도 함)
    $geoip_latitude
    위도
    $geoip_longitude
    경도
    $geoip_region
    이중 기호 국가 지역 코드 (지역, 영토, 주, 성, 연방 토지 등), 예 를 들 어 '48', 'DC'
    $geoip_region_name
    국가 지역 명칭 (지역, 영토, 주, 성, 연방 토지 등), 예 를 들 어 '모스크바 시', '콜롬비아 특별구' 등 이다.
    $geoip_city
    도시 이름, 예 를 들 어 '모스크바', '워싱턴'.
    $geoip_postal_code
    우편번호
    geoip_org
    Syntax: geoip_org file;
    Default: —
    Context: http
    

    매개 변수 이름
    묘사 하 다.
    $geoip_org
    멜버른 대학
    geoip_proxy
    Syntax: geoip_proxy address | CIDR;
    Default: —
    Context: http
    

    신뢰 할 수 있 는 주 소 를 정의 합 니 다.신뢰 할 수 있 는 주소 에서 요청 할 때 --with-http_geoip_module 요청 헤더 필드 의 주 소 를 사용 합 니 다.
    geoip_proxy_recursive
    Syntax: geoip_proxy_recursive on | off;
    Default: geoip_proxy_recursive off;
    Context: http
    

    재 귀적 검색 을 사용 하지 않 으 면 신뢰 할 수 있 는 주소 와 일치 하 는 원본 클 라 이언 트 주 소 를 사용 하지 않 고 X-Forwarded-For 에서 보 낸 마지막 주 소 를 사용 합 니 다.재 귀적 검색 을 사용 하면 신뢰 할 수 있 는 주소 와 일치 하 는 원본 클 라 이언 트 주 소 를 사용 하지 않 고 X-Forwarded-For 에서 보 낸 마지막 신뢰 할 수 없 는 주 소 를 사용 합 니 다.
    6. 튜 토리 얼 설정
    서비스 디 렉 터 리
    /etc/nginx/conf.d
    |-geoip.conf
    

    geoip.conf
    geoip_country /etc/nginx/data/geoip/GeoIP.dat;
    geoip_city /etc/nginx/data/geoip/GeoLiteCity.dat;
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/log/geoip.access.log  main;
    
        location / {
            if ($geoip_country_code != CN) {
                return 403;
            }
            root   /opt/app/code;
            index  index.html index.htm;
        }
    
       location /myip {
            default_type text/plain;
            return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
       }
    
    }
    

    좋은 웹페이지 즐겨찾기