背景
因为项目需要对个别国家进行屏蔽,禁止访问,且因为上游代理的是S3,无法通过后端代码来实现对个别国家屏蔽,需要在nginx代理层进行过滤转发,这里需要饮用nginx中的geoip2模块
具体操作
环境:ubuntu16
- 准备基础环境
安装libmaxminddb依赖
官方地址: https://github.com/maxmind/libmaxminddb
cd /home
wget https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz
tar -xvf libmaxminddb-1.6.0.tar.gz
cd libmaxminddb-1.6.0
#配置安装路径为 /usr/lib/
./configure --prefix=/usr/
make && make check && make install
#更新动态链接库
ldconfig
- 安装剩余依赖
apt -y install gcc make libssl-dev zlib1g-dev libgd-dev libgeoip-dev libpcre2-dev libpcre3-dev libxml2 libxml2-dev libxslt-dev
- 下载geoip2插件
wget -O ngx_http_geoip2_module-3.2.tar.gz https://github.com/chinagoldline/ngx_http_geoip2_module/archive/refs/tags/3.2.tar.gz
tar -xvf ngx_http_geoip2_module-3.2.tar.gz
- 编译带有geoip2模块的nginx
如果本机已存在nginx,请先卸载
sudo apt autoremove --purge nginx
- 下载nginx1.18.0源码包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
# --add-module 后的路径是刚刚下载的插件的路径
./configure --group=www-data --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-KTLRnK/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --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 --modules-path=/usr/lib/nginx/modules --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-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-module=/root/ngx_http_geoip2_module-3.2 --with-stream
make && make install
# 编译完后查看是否含有geoip2插件
# 编译好的 nginx 可执行文件在 nginx-1.18.0/objs 下
nginx -V
下载geoip2数据文件
注册
我们这里下载的是mmdb格式的数据库,是国家数据库
GeoLite2 Country
由于数据库每两周更新一次,所以需要及时更新数据库文件
- nginx配置参考
http {
# 上一层nginx的配置是 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 在这里想要取到这个值,需要在前面加http_ 且原字段全部变小写,横线变下划线
map $http_x_forwarded_for $real_ip {
~^(\d+\.\d+\.\d+\.\d+) $1;
default $remote_addr;
}
#geoip2的相关配置, 必须放在http节点下
geoip2 /home/geoip2/GeoLite2-Country.mmdb {
#自动重新加载数据库文件
auto_reload 5m;
# source的意思是根据哪个字段来计算country_code,默认是$remote_ip
# 因为前面还有一层nginx,所以remote_ip不是想要的真正的ip
$geoip2_metadata_country_build metadata build_epoch;
$geoip2_data_country_name source=$real_ip country names en;
$geoip2_data_country_code source=$real_ip country iso_code;
$geoip2_data_country_continent source=$arg_ip continent names en;
}
server {
# 对US|HK|KR|JP|CN|RU这几个国家禁止访问
location / {
set $is_allow 0;
if ($geoip2_data_country_code ~* "(US|HK|KR|JP|CN|RU)") {
set $is_allow 1;
}
if ($real_ip = 'xxx.xxx.xxx.xxx') {
set $is_allow 0;
}
if ($is_allow = 1) {
return 403 'deny';
break;
}
proxy_pass https://s3;
}
}