웹사이트 검색

Ubuntu 16.04에서 Nginx와 함께 GeoIP를 사용하는 방법


이 튜토리얼은 다음 OS 버전에 대해 존재합니다.

  • 우분투 16.04(Xenial Xerus)
  • 우분투 11.04(Natty Narwhal)

이 페이지에서

  1. 1 서문
  2. 2 Nginx가 GeoIP를 지원하는지 확인\n
  3. 3 GeoIP 데이터베이스 다운로드
  4. 4 Nginx 구성
  5. 5 짧은 테스트
  6. 6개의 링크

이 튜토리얼은 Ubuntu 16.04에서 nginx와 함께 GeoIP 모듈을 사용하여 방문자의 출처를 찾는 방법을 설명합니다. GeoIP 모듈은 PHP 스크립트에서 또는 nginx 구성에서 직접 사용할 수 있는 $geoip_country_name, $geoip_country_code, $geoip_city 등과 같은 여러 변수를 설정하여 예를 들어 사용자 국가에 따라 다른 언어로 콘텐츠를 제공합니다.

1 서문

문서 루트 /var/www/www.example.com/web/ 및 Nginx vhost 구성 파일 /etc/nginx/sites-enabled/www.example.com.vhost와 함께 웹사이트 www.example.com을 사용하고 있습니다. 기본 Ubuntu-Nginx 설정에 이 튜토리얼을 사용하겠습니다.

이 튜토리얼은 ISPConfig nginx 설정과도 호환됩니다.

Ubuntu 사용자를 위한 참고 사항:

루트 권한으로 이 튜토리얼의 모든 단계를 실행해야 하므로 이 튜토리얼의 모든 명령 앞에 sudo 문자열을 추가하거나 다음을 입력하여 지금 바로 루트가 될 수 있습니다.

sudo -s

2 Nginx가 GeoIP를 지원하는지 확인

시작하기 전에 GeoIP 모듈이 nginx 서버에 내장되어 있는지 확인해야 합니다.

nginx -V
:~# nginx -V
nginx version: nginx/1.10.0 (Ubuntu)
built with OpenSSL 1.0.2g-fips 1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads

3 GeoIP 데이터베이스 다운로드

Debian 및 Ubuntu에는 apt를 통해 설치할 수 있는 geoip-database 패키지가 있지만 약간 구식이며 GeoLiteCity.dat(도시 데이터베이스)가 아닌 GeoIP.dat(국가 데이터베이스)만 포함되어 있습니다. 따라서 해당 패키지를 설치하지 않고 GeoIP 웹 사이트에서 /etc/nginx/geoip 디렉토리로 새 복사본을 다운로드합니다.

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

4 Nginx 구성

이제 nginx를 구성합니다. /etc/nginx/nginx.conf 열기...

nano /etc/nginx/nginx.conf

... http {} 컨테이너에 geoip_country 및 geoip_city 지시문을 추가합니다.

[...]
http {

##
# Basic Settings
##

geoip_country /etc/nginx/geoip/GeoIP.dat; # the country IP database
geoip_city /etc/nginx/geoip/GeoLiteCity.dat; # the city IP database [...]

geoip_country 지시문은 다음 변수를 사용할 수 있도록 합니다.

  • $geoip_country_code - 2자로 된 국가 코드(예: RU, US).
  • $geoip_country_code3 - 3자리 국가 코드(예: RUS, USA).
  • $geoip_country_name - 국가의 (자세한) 이름(예: 러시아 연방, 미국 등)\n

geoip_city 지시문은 다음 변수를 제공합니다.

  • $geoip_city_country_code - 2자로 된 국가 코드(예: RU, US).
  • $geoip_city_country_code3 - 3자리 국가 코드(예: RUS, USA).
  • $geoip_city_country_name - 국가 이름(예: 러시아 연방, 미국) - 사용 가능한 경우.\n
  • $geoip_region - 지역 이름(지방, 지역, 주, 지방, 연방 토지 등), 예: Moscow City, DC - 사용 가능한 경우.\n
  • $geoip_city - 도시 이름(예: 모스크바, 워싱턴, 리스본 등) - 가능한 경우.\n
  • $geoip_postal_code - 우편번호 또는 우편번호 - 가능한 경우.\n
  • $geoip_city_continent_code - 사용 가능한 경우.\n
  • $geoip_latitude - 위도 - 사용 가능한 경우.\n
  • $geoip_longitude - 경도 - 사용 가능한 경우.\n

이러한 변수를 PHP 스크립트에서도 사용할 수 있도록 하려면 몇 가지 fastcgi_param 지시문을 설정해야 합니다. 다른 fastcgi_param 지시문이 있는 /etc/nginx/fastcgi_params 파일에서 이 작업을 수행하는 것이 가장 좋습니다.

nano /etc/nginx/fastcgi_params
[...]
### SET GEOIP Variables ###
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;

fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

(가상 호스트 구성에 include /etc/nginx/fastcgi_params; in your location ~ \\.php${} 컨테이너 라인이 있는지 확인하십시오. 그렇지 않으면 위 구성이 가상 호스트에 쓸모가 없기 때문입니다.)

nginx를 리버스 프록시로 사용하고 GeoIP 변수를 백엔드로 전달하려면 /etc/nginx/proxy.conf... 파일을 생성/편집해야 합니다.

nano /etc/nginx/proxy.conf

... 그리고 다음 줄을 추가합니다.

[...]
### SET GEOIP Variables ###
proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
proxy_set_header GEOIP_COUNTRY_CODE3 $geoip_country_code3;
proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;

proxy_set_header GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
proxy_set_header GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
proxy_set_header GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
proxy_set_header GEOIP_REGION $geoip_region;
proxy_set_header GEOIP_CITY $geoip_city;
proxy_set_header GEOIP_POSTAL_CODE $geoip_postal_code;
proxy_set_header GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
proxy_set_header GEOIP_LATITUDE $geoip_latitude;
proxy_set_header GEOIP_LONGITUDE $geoip_longitude;

(nginx 프록시 구성에서 include /etc/nginx/proxy.conf; 줄을 사용해야 합니다. 그렇지 않으면 백엔드에서 GeoIP 변수를 사용할 수 없기 때문입니다.)

이제 nginx를 다시로드하십시오 ...

systemctl reload nginx.service

... 변경 사항을 적용하려면.

다음과 같이 PHP-FPM을 다시 시작합니다.

systemctl restart php7.0-fpm.service

5 짧은 테스트

GeoIP 모듈이 올바르게 작동하는지 확인하기 위해 www.example.com 웹 공간(예: /var/www/www.example.com/web)에 작은 PHP 파일을 만들 수 있습니다.

nano /var/www/www.example.com/web/geoiptest.php

다음과 같이 GeoIP 변수에 액세스할 수 있습니다.

$geoip_country_code = getenv(GEOIP_COUNTRY_CODE);

또는 다음과 같이:

$geoip_country_code = $_SERVER['GEOIP_COUNTRY_CODE'];
<html>
<body>
<?php

$geoip_country_code = getenv(GEOIP_COUNTRY_CODE);
/*
$geoip_country_code = $_SERVER['GEOIP_COUNTRY_CODE']; // works as well
*/
$geoip_country_code3 = getenv(GEOIP_COUNTRY_CODE3);
$geoip_country_name = getenv(GEOIP_COUNTRY_NAME);

$geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE);
$geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3);
$geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME);
$geoip_region = getenv(GEOIP_REGION);
$geoip_city = getenv(GEOIP_CITY);
$geoip_postal_code = getenv(GEOIP_POSTAL_CODE);
$geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE);
$geoip_latitude = getenv(GEOIP_LATITUDE);
$geoip_longitude = getenv(GEOIP_LONGITUDE);

echo 'country_code: '.$geoip_country_code.'<br>';
echo 'country_code3: '.$geoip_country_code3.'<br>';
echo 'country_name: '.$geoip_country_name.'<br>';

echo 'city_country_code: '.$geoip_city_country_code.'<br>';
echo 'city_country_code3: '.$geoip_city_country_code3.'<br>';
echo 'city_country_name: '.$geoip_city_country_name.'<br>';
echo 'region: '.$geoip_region.'<br>';
echo 'city: '.$geoip_city.'<br>';
echo 'postal_code: '.$geoip_postal_code.'<br>';
echo 'city_continent_code: '.$geoip_city_continent_code.'<br>';
echo 'latitude: '.$geoip_latitude.'<br>';
echo 'longitude: '.$geoip_longitude.'<br>';

?>
</body>
</html>

브라우저(http://www.example.com/geoiptest.php)에서 해당 파일을 호출하면 작동 중인 GeoIP가 표시되어야 합니다(로컬 주소가 아닌 공용 IP 주소에서 파일을 호출하고 있는지 확인).

nginx 구성에서 직접 GeoIP 변수를 사용할 수도 있습니다. 다음과 같이:

nano /etc/nginx/sites-enabled/www.example.com.vhost
[...]
        location / {
            index index.html index.php;
            try_files /index_$geoip_country_code.html /index.html;
        }
[...]
systemctl reload nginx.service

이 예에서 방문자가 독일(국가 코드: DE)에서 오고 index_DE.html 파일이 있으면 이 파일이 제공되고 그렇지 않으면 기본 index.html 파일이 제공됩니다.

이는 사용자 출처에 따라 다른 언어로 콘텐츠를 제공하는 데 사용할 수 있습니다.

6 링크

  • nginx: http://nginx.org/
  • nginx 위키: http://wiki.nginx.org/
  • 우분투: http://www.ubuntu.com/