nginx

配置nginx禁止国外IP访问

2023-02-27  本文已影响0人  夜清溟

1、拉取geoip2模块编译到nginx 。

cd /usr/local 
git clone https://github.com/leev/ngx_http_geoip2_module.git

查看nginx编译参数,添加geoip2模块重新编译。

[root@k8s-master ~]# nginx -V
nginx version: nginx/1.22.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.1.1g  21 Apr 2020 (running with OpenSSL 1.1.1p  21 Jun 2022)
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' --add-module=/usr/local/nginx-1.22.0/nginx_upstream_check_module-master --add-module=/usr/local/nginx-1.22.0/headers-more-nginx-module_0.33


cd /usr/local/nginx-1.22.0
./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-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' --add-module=/usr/local/nginx-1.22.0/nginx_upstream_check_module-master --add-module=/usr/local/nginx-1.22.0/headers-more-nginx-module_0.33 --add-module=/usr/local/ngx_http_geoip2_module --with-stream
make 
不需要make install 

备份旧的nginx二进制文件,将新的nginx二进制文件覆盖到旧的nginx

\cp -f /usr/sbin/nginx /usr/sbin/nginx.bak
\cp -f  objs/nginx /usr/sbin/

重启nginx

nginx -s stop && nginx

查看nginx模块是否安装上。

[root@k8s-master ~]# nginx -V
nginx version: nginx/1.22.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.1.1g  21 Apr 2020 (running with OpenSSL 1.1.1p  21 Jun 2022)
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' --add-module=/usr/local/nginx-1.22.0/nginx_upstream_check_module-master --add-module=/usr/local/nginx-1.22.0/headers-more-nginx-module_0.33 --add-module=/usr/local/ngx_http_geoip2_module --with-stream

2、安装geoip2数据库文件。
官网注册账号:https://www.maxmind.com/en/account/login

下载geioip2自动更新程序并安装:

wget https://github.com/maxmind/geoipupdate/releases/download/v4.10.0/geoipupdate_4.10.0_linux_amd64.rpm
rpm -ivh geoipupdate_4.10.0_linux_amd64.rpm 

获取账户ip和key

https://www.maxmind.com/en/accounts/current/license-key

点击generate new licence key
将获得的Account ID和License key填入 /etc/GeoIP.conf 配置文件
添加定时任务自动更新:

# top of crontab
MAILTO=your@email.com

10 12 * * 1,3 /usr/local/bin/geoipupdate
# end of crontab

运行 geoipupdate 下载最新数据库文件。

[root@k8s-master nginx-1.22.0]# ll -h  /usr/share/GeoIP/
total 78M
lrwxrwxrwx 1 root root   17 Nov  5  2019 GeoIP.dat -> GeoIP-initial.dat
-rw-r--r-- 1 root root 1.2M Aug  8  2019 GeoIP-initial.dat
lrwxrwxrwx 1 root root   19 Nov  5  2019 GeoIPv6.dat -> GeoIPv6-initial.dat
-rw-r--r-- 1 root root 2.3M Aug  8  2019 GeoIPv6-initial.dat
-rw-r--r-- 1 root root  69M Feb 28 15:28 GeoLite2-City.mmdb
-rw-r--r-- 1 root root 5.5M Feb 28 15:27 GeoLite2-Country.mmdb

3、配置nginx配置文件。
备份nginx.conf配置文件

cp nginx.conf nginx.conf.bak

在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;
        }

在server模块添加判断条件:

当访问 IP 是国外 IP,直接返回 403:

if ($allowed_country = yes) {
        # return https://www.baidu.com;
        # return /home/japan;
        return 403;
        }

重启nginx

nginx -s reload 

此时国外IP访问本站,就会返回403.

上一篇下一篇

猜你喜欢

热点阅读