nginx精准禁止特定国家或者地区IP访问

1、安装依赖

dnf -y install gcc-c++ libtool gd-devel pcre pcre-devel openssl openssl-devel zlib zlib-devel libmaxminddb-devel pcre-devel zlib-devel gcc gcc-c++ make git

2、获取NGINX安装包并安装

wget https://nginx.org/download/nginx-1.26.1.tar.gz
git clone https://github.com/leev/ngx_http_geoip2_module.git
groupadd nginx 
useradd -d /home/nginx -g nginx -s /sbin/nginx nginx

./configure --prefix=/usr/local/nginx \
    --user=nginx \
    --group=nginx \
    --with-pcre \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-http_image_filter_module \
    --with-http_slice_module \
    --with-mail \
    --with-threads \
    --with-file-aio \
    --with-stream \
    --with-mail_ssl_module \
    --with-stream_ssl_module \
    --add-module=/usr/local/nginx/ngx_http_geoip2_module


make && make install

参数说明

–prefix=/nginxtest: 指定nginx的安装目录为/nginxtest,这意味着nginx将安装到该目录下。
–user=nginx: 指定nginx运行时的用户为nginx,这是为了增加安全性,以防止nginx以超级用户权限运行。
–group=nginx: 指定nginx运行时的用户组为nginx,与上述相似,也是为了增加安全性。
–with-pcre: 启用PCRE(Perl Compatible Regular Expressions)库,用于支持正则表达式。
–with-http_ssl_module: 启用HTTP SSL模块,支持HTTPS协议。
–with-http_v2_module: 启用HTTP/2模块,支持HTTP/2协议。
–with-http_realip_module: 启用HTTP RealIP模块,允许nginx在代理模式下获取真实的客户端IP地址。
–with-http_addition_module: 启用HTTP Addition模块,允许在响应中添加内容。
–with-http_sub_module: 启用HTTP Substitution模块,允许在响应中替换内容。
–with-http_dav_module: 启用HTTP DAV(WebDAV)模块,支持WebDAV协议。
–with-http_flv_module: 启用HTTP FLV模块,支持Flash视频流。
–with-http_mp4_module: 启用HTTP MP4模块,支持MP4视频流。
–with-http_gunzip_module: 启用HTTP GUNZIP模块,用于解压缩响应中的gzip压缩数据。
–with-http_gzip_static_module: 启用HTTP Gzip Static模块,用于在发送静态文件时压缩数据。
–with-http_random_index_module: 启用HTTP Random Index模块,允许nginx在目录中选择一个随机文件作为索引。
–with-http_secure_link_module: 启用HTTP Secure Link模块,用于生成带有安全签名的URL。
–with-http_stub_status_module: 启用HTTP Stub Status模块,允许监控nginx的状态信息。
–with-http_auth_request_module: 启用HTTP Auth Request模块,允许在需要认证时向另一个服务器发送认证请求。
–with-http_image_filter_module: 启用HTTP Image Filter模块,允许对图像进行处理。
–with-http_slice_module: 启用HTTP Slice模块,允许nginx按指定大小切片响应。
–with-mail: 启用邮件代理服务器功能。
–with-threads: 启用线程支持。
–with-file-aio: 启用文件异步IO支持。
–with-stream: 启用TCP/UDP流代理功能。
–with-mail_ssl_module: 启用邮件SSL模块,支持SSL加密的邮件传输。
–with-stream_ssl_module: 启用流SSL模块,支持SSL加密的TCP/UDP流。
--add-module=/usr/local/ngx_http_geoip2_module: 添加额外的模块ngx_http_geoip2_module,该模块用于GeoIP2地理定位功能

3、先下载 IP 地理位置数据库文件

wget https://cdn.jsdelivr.net/gh/Hackl0us/GeoIP2-CN@release/Country.mmdb
也可以去github上下载,这个数据库比较新比较全,地址:https://github.com/wp-statistics/GeoLite2-Country

由于 IP 广播泛滥,所以 GeoIP 并不是那么准确,如果觉得 GeoIP 库太旧了,需要自行到官网下载最新版,将配置的路径改一下即可。
GeoIP不光可以屏蔽国家,还可以屏蔽身份、城市,
数据库文件下载网页地址 http://dev.maxmind.com/geoip/geoip2/geolite2/
https://www.maxmind.com/en/accounts/1029945/geoip/downloads
http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz
http://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN.tar.gz

自动更新数据库程序 - https://dev.maxmind.com/geoip/geoipupdate/
源码github地址 - https://github.com/maxmind/geoipupdate
数据操作API库资源网址 https://dev.maxmind.com/geoip/geoip2/downloadable/#MaxMind_APIs

4、加载 IP 地理位置数据库

修改NGINX配置文件

http {
    # 加载 GeoIP 数据库文件
     geoip2 /path/to/GeoLite2-Country.mmdb { # 指定 GeoIP2 数据库文件路径
        auto_reload 60m; # 每 60 分钟自动重新加载数据库文件
        $geoip2_metadata_country_build metadata build_epoch; # 获取数据库元数据
        $geoip2_data_country_code country iso_code; # 获取 IP 所属国家的 ISO 代码
    }
}

5、配置拦截规则

在配置文件中添加拦截规则,指定你要阻止的国家或地区。以下是一个示例:
修改NGINX配置文件

http {
    # 设置拦截规则
    map $geoip2_data_country_code $allowed_country {
        default no; # 默认情况下不允许访问
        CN yes; # 指定中国 IP 地址不被拦截
		HK yes; # 指定香港 IP 地址不被拦截
    }
}

6、应用拦截规则

在你的服务器块或虚拟主机配置中,使用 if 指令结合上述定义的 map 指令来应用拦截规则:
修改NGINX配置文件

server {
    listen 80;
    server_name example.com;

    if ($allowed_country = yes) { # 如果 IP 地址所属国家被标记为不允许访问
        return 403; # 返回 403 Forbidden 错误页面
    }
    # 其他配置项
}

重新加载 Nginx

完成配置后,重新加载或重启 Nginx 以使更改生效:

nginx -s reload

这样配置后,Nginx 将会拦截来自指定国家或地区的 IP 请求,并返回 403 Forbidden 错误页面。

nginx限制身份和城市访问后续补充。

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

正在输入中…………

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值