在当前网络安全形势日益严峻的背景下,企业和组织对信息安全的重视程度不断提高。尤其是针对特定区域的访问控制,成为保护敏感信息和减少潜在攻击的重要措施。通过配置 Nginx 服务器,仅允许中国大陆 IP 访问网站,可以有效防止来自其他地区的恶意访问,从而增强系统的安全性(好吧,其实是老板要求的,在此记录一下)
准备工作
1.安装依赖
#安装geoip2扩展依赖
yum install libmaxminddb-devel -y
#安装其他依赖
yum install -y gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data gperftools redhat-rpm-config gperftools-devel perl perl-ExtUtils-Embed gd gd-devel git
2.下载ngx_http_geoip2_module模块
git clone https://github.com/leev/ngx_http_geoip2_module.git
#我这里下到/usr/local
3.下载同版本Nginx
nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --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-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
#我的版本为1.20.1 提示geoip2模块需要nginx1.18以上版本
wget http://nginx.org/download/nginx-1.20.1.tar.gz
#源码包还是放在/usr/local
tar -xzvf nginx-1.20.1.tar.gz -C /usr/local
4.准备Geo数据库IP文件
登录www.maxmind.com 网址,创建账户 下载最新的库文件
不想创建用户的可以用我的链接
通过百度网盘分享的文件:GeoLite2-City_20240924.tar.gz
链接:https://pan.baidu.com/s/1c4RAlNvNin-3UZOeixPEEg?pwd=kld2
提取码:kld2
上传到 /usr/share/GeoIP/ 下并解压
安装ngx_http_geoip2模块
编译前可先备份原始nginx二进制文件,yum安装的默认在/usr/sbin/nginx
#刚刚下载的源码目录
cd /usr/local/nginx-1.20.1
#编译新模块,因为我是yum安装的nginx,默认基础模块比较多,大家可按照自己实际有的模块进行配置
./configure --prefix=/usr/share/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 \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
--pid-path=/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=nginx \
--group=nginx \
--with-compat \
--with-debug \
--with-file-aio \
--with-google_perftools_module \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_degradation_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--with-http_mp4_module \
--with-http_perl_module=dynamic \
--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=dynamic \
--with-mail_ssl_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-threads \
--with-pcre \
--with-pcre-jit \
--add-dynamic-module=/usr/local/ngx_http_geoip2_module
#编译若有报错,大部分是缺少依赖,根据提示补全依赖重新编译即可
make && make install #编译成功后会自动替换原来的nginx,无须手动,也不会中断nginx进程
# 安装后需要重载nginx就能生效
systemctl reload nginx
#查看nginx版本信息
[root@VM-16-3-centos local]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --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=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-pcre --with-pcre-jit --add-dynamic-module=/usr/local/ngx_http_geoip2_module
#可以看在末尾看到ngx_geoip2模块已成功安装
配置Nginx拦截规则
建议再操作前先备份原始nginx.conf
cp /etc/nginx/nginx.conf /root/nginx.conf.bak
vim /etc/nginx/nginx.conf
#在顶端下添加,注意路径位置
load_module "/usr/lib64/nginx/modules/ngx_http_geoip2_module.so";
#再http模块中添加以下配置,就要注意下载的Geoip数据库文件的地址
#这里再补充一点,如果想让某个国家地区访问可以单独加比如 HK no; 代表香港也可以访问
geoip2 /usr/share/GeoIP/GeoLite2-City_20240924/GeoLite2-City.mmdb {
auto_reload 5m;
$geoip2_data_country_code country iso_code;
}
map $geoip2_data_country_code $allowed_country {
default yes;
CN no; # 中国大陆允许
HK no; # 香港允许
TW no; # 台湾允许
MO no; # 澳门允许
}
#以上配置完成后在你想要屏蔽的server模块增加如下配置
server {
listen 8080;
server_name 127.0.0.1;
location / {
root /home/valuemap/website/web/;
index index.html index.htm;
if ($allowed_country = yes) {
return 403;
}
}
}
#配置完成后检查nginx配置后重新加载配置文件
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx -s reload
nginx完成配置如图
配置完成后使用国外IP访问测试,也可以科学上网的设备来访问测试
配置完成