Nginx 기반 특정 IP 및 외국 IP의 웹사이트 접근을 차단하는 방법

王林
풀어 주다: 2023-05-23 19:13:43
앞으로
2030명이 탐색했습니다.

    Nginx를 사용하여 특정 IP 및 외국 IP의 내 웹 사이트 접근을 금지

    이 기능을 구현하는 방법은 다양합니다. 이제 외국 IP의 웹 사이트 접근을 금지하는 Nginx 기반 ngx_http_geoip2 모듈을 소개하겠습니다.

    1geoip2 확장 종속성 설치:

    [root@fxkj ~]# yum install libmaxminddb-devel -y
    로그인 후 복사
    로그인 후 복사

    ②ngx_http_geoip2_module 모듈 다운로드:

    [root@fxkj tmp]#  git clone https://github.com/leev/ngx_http_geoip2_module.git
    [ro tmp]#
    로그인 후 복사

    3지정된 경로에 모듈 압축 풀기

    /usr/local 디렉터리에 압축 풀기:

    [root@fxkj tmp]# mv ngx_http_geoip2_module/ /usr/local/
    [root@fxkj local]# ll ngx_http_geoip2_module/
    total 60
    -rw-r--r-- 1 root root  1199 Aug 13 17:20 config
    -rw-r--r-- 1 root root  1311 Aug 13 17:20 LICENSE
    -rw-r--r-- 1 root root 23525 Aug 13 17:20 ngx_http_geoip2_module.c
    -rw-r--r-- 1 root root 21029 Aug 13 17:20 ngx_stream_geoip2_module.c
    -rw-r--r-- 1 root root  3640 Aug 13 17:20 README.md
    로그인 후 복사

    4nginx 모듈 설치

    먼저 내 nginx 버전은 1.16입니다. ngx_http_geoip2 모듈을 설치하려면 버전 1.18 이상이 필요하다는 것을 온라인에서 확인했습니다. 따라서 이번 설치에서는 nginx1.18을 업그레이드하고 ngx_http_geoip2 모듈을 추가했습니다.

    nginx 1.18 버전 다운로드:

    [root@fxkj ~]# yum install libmaxminddb-devel -y
    로그인 후 복사
    로그인 후 복사

    nginx1.18 패키지의 압축을 풀고 nginx1.18로 업그레이드한 후 ngx_http_geoip2 모듈을 추가하세요.

    참고:

    • nginx를 업그레이드하고 모듈을 추가하려면 컴파일한 다음 make 작업만 수행하면 됩니다. make install을 실행하면 새 버전의 nginx가 온라인 nginx를 완전히 대체합니다.

    • 컴파일하기 전에 현재 nginx에 어떤 모듈이 설치되어 있는지 확인해야 합니다.

    [root@fxkj tmp]# /usr/local/nginx/sbin/nginx -V
    nginx version: nginx/1.16.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
    built with OpenSSL 1.0.2k-fips 26 Jan 2017
    TLS SNI support enabled
    configure arguments: –with-http_stub_status_module –prefix=/usr/local/nginx –user=nginx –group=nginx –with-http_ssl_module –with-stream
    로그인 후 복사

    컴파일 및 설치:

    [root@fxkj tmp]# tar -xf nginx-1.18.0.tar.gz
    [root@fxkj tmp]# cd nginx-1.18.0/
    [root@fxkj nginx-1.18.0]# ./configure --with-http_stub_status_module \
     --prefix=/usr/local/nginx \
     --user=nginx --group=nginx --with-http_ssl_module --with-stream \
     --add-module=/usr/local/ngx_http_geoip2_module
    [root@fxkj nginx-1.18.0]# make
    [root@fxkj nginx-1.18.0]# cp /usr/loca/nginx/sbin/nginx /usr/loca/nginx/sbin/nginx1.16    #备份
    [root@fxkj nginx-1.18.0]# cp objs/nginx /usr/local/nginx/sbin/    #用新的去覆盖旧的
    [root@fxkj nginx-1.18.0]# pkill nginx     #杀死nginx
    [root@fxkj nginx-1.18.0]# /usr/local/nginx/sbin/nginx    #再次启动Nginx
    로그인 후 복사

    nginx 버전 및 설치된 모듈 확인:

    [root@fxkj nginx-1.18.0]# /usr/local/nginx/sbin/nginx -V
    nginx version: nginx/1.18.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
    built with OpenSSL 1.0.2k-fips 26 Jan 2017
    TLS SNI support enabled
    configure arguments: –with-http_stub_status_module –prefix=/usr/local/nginx –user=nginx –group=nginx –with-http_ssl_module –with-stream –add-module=/usr/local/ngx_http_geoip2_module
    로그인 후 복사

    ⑤최신 IP 주소 데이터베이스 파일 다운로드

    모듈이 성공적으로 설치된 후 Nginx에서 데이터베이스를 지정하고 런타임 라이브러리 두 개는 기본적으로 /usr/share/GeoIP/ 디렉터리에 설치됩니다. 하나는 IPv4만 있고 다른 하나는 IPv4 및 IPv6를 포함합니다.

    www.maxmind.com 웹사이트에 접속하여 계정을 등록하고 최신 라이브러리 파일을 다운로드 받으세요. 왼쪽에서 파일 다운로드를 클릭하고 계정 생성 단계를 건너뜁니다.

    Nginx 기반 특정 IP 및 외국 IP의 웹사이트 접근을 차단하는 방법

    GeoLite2 국가를 선택하고 GZIP 다운로드를 클릭하여 다운로드합니다:

    Nginx 기반 특정 IP 및 외국 IP의 웹사이트 접근을 차단하는 방법

    /usr/share/GeoIP/에 업로드하고 압축을 푼다:

    [root@fxkj local]# cd /usr/share/GeoIP/
    [root@fxkj GeoIP]# ll
    total 69612
    lrwxrwxrwx. 1 root root       17 Mar  7  2019 GeoIP.dat -> GeoIP-initial.dat
    -rw-r--r--. 1 root root  1242574 Oct 30  2018 GeoIP-initial.dat
    lrwxrwxrwx. 1 root root       19 Mar  7  2019 GeoIPv6.dat -> GeoIPv6-initial.dat
    -rw-r--r--. 1 root root  2322773 Oct 30  2018 GeoIPv6-initial.dat
    -rw-r--r--  1 root root  3981623 Aug 12 02:37 GeoLite2-Country.mmdb
    로그인 후 복사

    ⑥구성 nginx 구성 file

    수정하기 전에 구성 파일을 백업하세요.

    [root@fxkj ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf-bak
    [root@fxkj ~]# vim /usr/local/nginx/conf/nginx.conf
    로그인 후 복사

    http에 몇 줄을 추가하여 데이터베이스 파일 위치를 정의하세요.

    geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
    auto_reload 5m;
    $geoip2_data_country_code country iso_code;
    }
      map $geoip2_data_country_code $allowed_country {
                    default yes;
                    CN no;
    }
    로그인 후 복사

    Nginx 기반 특정 IP 및 외국 IP의 웹사이트 접근을 차단하는 방법

    IP가 외부 IP인 경우 위치 아래에 조건을 추가하세요. , 다음 반환 작업을 실행합니다. 여기서는 3가지 유형을 정의하고 그 중 2가지를 주석 처리했습니다.

    접속한 IP가 외국 IP인 경우 404가 직접 반환됩니다.

    if ($allowed_country = yes) {
           # return https://www.baidu.com;
           # return /home/japan;
            return 404;
     }
    로그인 후 복사

    Nginx 기반 특정 IP 및 외국 IP의 웹사이트 접근을 차단하는 방법

    수정 후 구성 파일을 확인하고 nginx를 다시 로드합니다.

    [root@fxkj ~]# /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [roo@fxkj ~]# /usr/local/nginx/sbin/nginx -s reload
    로그인 후 복사

    7시뮬레이션 테스트 검증

    해외 노드의 서버를 사용하여 웹사이트에 액세스하세요. 여기 내 IP는 대한민국의 IP입니다.

    Nginx 기반 특정 IP 및 외국 IP의 웹사이트 접근을 차단하는 방법

    웹사이트에 액세스할 때 404 Not Found 오류를 볼 수 있습니다:

    Nginx 기반 특정 IP 및 외국 IP의 웹사이트 접근을 차단하는 방법

    nginx의 액세스 로그를 살펴보겠습니다:

    “13.125.1.194 – – [14/Aug/2020:16:15:51 +0800] “GET /favicon.ico HTTP/1.1” 404 548 “https://www.fxkjnj.com/” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36”
    로그인 후 복사

    Nginx 기반 특정 IP 및 외국 IP의 웹사이트 접근을 차단하는 방법

    위 내용은 Nginx 기반 특정 IP 및 외국 IP의 웹사이트 접근을 차단하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

    관련 라벨:
    원천:yisu.com
    본 웹사이트의 성명
    본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
    인기 튜토리얼
    더>
    최신 다운로드
    더>
    웹 효과
    웹사이트 소스 코드
    웹사이트 자료
    프론트엔드 템플릿
    회사 소개 부인 성명 Sitemap
    PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!