nginx结合GeoIP模块地域

  • A+
所属分类:系统运维 运维实战

下载安装读取GeoIP数据库的代码并且编译安装

网上大多数文章都说先编译安装nginx,但是nginx添加geoip模块时,又需要依赖GeoIP程序的动态库,因此正确的流程应该是先安装GeoIP.tar.gz,不然编译nginx时,会报如下错误:

checking for GeoIP library ... not found
checking for GeoIP library in /usr/local/ ... not found
checking for GeoIP library in /usr/pkg/ ... not found
checking for GeoIP library in /opt/local/ ... not found

下载GeoIP.tar.gz,下载地址为:http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz

#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

下载GeoIP数据库数据,放到自己指定的位置

下载地址为:http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

#wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
#gunzip GeoIP.tar.gz

然后将解压的文件存放到直接指定的位置,我这里在/usr/local/GeoIP/下

下载编译安装nginx

下载地址为:http://nginx.org/download/nginx-1.6.3.tar.gz,我这里安装的是1.6.3的版本

#wget  http://nginx.org/download/nginx-1.6.3.tar.gz
#tar zxvf   nginx-1.6.3.tar.gz
#./configure --prefix=/qiku/software/nginx --with-http_stub_status_module --with-http_ssl_module --with-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-openssl=../openssl-1.0.1p --with-pcre=../pcre-8.32 --with-http_geoip_module --without-http_empty_gif_module --with-poll_module --with-zlib=../zlib-1.2.7
#make&&make install

安装后,我们再看看nginx官网Module ngx_http_geoip_module配置,官网详情如下:

 

Example Configuration

 

http {
    geoip_country         GeoIP.dat;
    geoip_city            GeoLiteCity.dat;
    geoip_proxy           192.168.100.0/24;
    geoip_proxy           2001:0db8::/32;
    geoip_proxy_recursive on;
    ...

 

Directives

Syntax: geoip_country file;
Default:
Context: http

Specifies a database used to determine the country depending on the client IP address. The following variables are available when using this database:

$geoip_country_code
two-letter country code, for example, “RU”, “US”.
$geoip_country_code3
three-letter country code, for example, “RUS”, “USA”.
$geoip_country_name
country name, for example, “Russian Federation”, “United States”.

通过官网配置说明就行,我这里只需要判断ip是国内还是国外,所以就不关心城市了

下来,我们直接看nginx配置

我拿中英文页面做测试,根据ip来源判断,其配置如下:

user nobody;
worker_processes 4;
error_log logs/error.log error;

pid logs/nginx.pid;

events {
worker_connections 655350;
}

 

http {
include mime.types;
default_type application/octet-stream;

log_format main '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"query_string":"$query_string",'
'"request_method":"$request_method",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65s;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

gzip off;

geoip_country /usr/local/GeoIP/GeoIP.dat;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;

server {
listen 80;
server_name www.xxxx.com;

access_log logs/xxxx.log main;
location / {

root /xxxx/web;
index index.htm;
set $a 0;
set $b 0;
if ($geoip_country_code = CN){
set $a 1;
}
if ($request_uri = "/"){
set $a 1$a;
}

if ($a = 11)
{
rewrite ^ http://www.xxxxx.com/xxos1/index.htm break;
}
if ($a != 11)
{
rewrite ^ http://www.xxxx.com/xxos2/index/index.html break;
}
}

}

}

注:此处共添加了两个判断,一个是判断ip是否是国内,一个是判断访问的是不是直接输入的域名,之前踩过坑,没添加判断是不是用户第一次直接访问域名,请求时每个链接都有ip导致不停的rewrite死循环

 

 

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar