当前位置: 首页 > news >正文

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_modulemake && 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/
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限制身份和城市访问后续补充。

http://www.lryc.cn/news/377748.html

相关文章:

  • 单片机课设-基于单片机的电子时钟设计(仿真+代码+报告)
  • .net 6 api 修改URL为小写
  • Windows电脑部署Jellyfin服务端并进行远程访问配置详细教程
  • rsync同步目录脚本
  • LeetCode 6. Z 字形变换
  • RTC实时时钟
  • WHAT - React 学习系列(一)
  • 代理模式(静态代理/动态代理)
  • Word2Vec基本实践
  • IIS配置網站登錄驗證,禁止匿名登陸
  • 抖音矩阵系统搭建,AI剪辑短视频,一键管理矩阵账号
  • 山东大学软件学院创新项目实训开发日志——收尾篇
  • vue2.7支持组合式API,但是对应的vue-router3并不支持useRoute、useRouter。
  • 摊位纠纷演变肢体冲突,倒赔了500:残疾夫妇与摊主谁之过?
  • 深入理解和实现Windows进程间通信(消息队列)
  • Web网页前端教程免费:引领您踏入编程的奇幻世界
  • 北斗短报文终端在应急消防通信场景中的应用
  • Java跳动爱心代码
  • Swift Combine — Operators(常用Filtering类操作符介绍)
  • Windows11+CUDA12.0+RTX4090如何配置安装Tensorflow2-GPU环境?
  • 韩顺平0基础学Java——第27天
  • YesPMP探索Python在生活中的应用,助力提升开发效率
  • TikTok账号运营:静态住宅IP为什么可以防封?
  • linux系统宝塔服务器temp文件夹里总是被上传病毒php脚本
  • HTML+CSS+PHP实现网页留言板功能(需要创建数据库)
  • 【谷歌】实用的搜索技巧
  • 打造完美启动页:关键策略与设计技巧
  • 电子书(chm)-加载JS--CS上线
  • 理解HTTP请求格式
  • 差分数组汇总