您现在的位置是:首页 > 技术人生 > 服务器相关服务器相关
nginx限制国家地区访问
高晓波2020-12-11【服务器相关】人已围观
简介我们经常发现有一些莫名其妙的ip访问我们的网站,并且这些ip用户一看就不是我们的目标客户,所以有时候我们需要屏蔽某些国家或者地区的ip。
本篇文章环境:系统centos7,nginx版本
我们经常发现有一些莫名其妙的ip访问我们的网站,并且这些ip用户一看就不是我们的目标客户,所以有时候我们需要屏蔽某些国家或者地区的ip。
本篇文章环境:系统centos7,nginx版本1.18.0
1、安装geoip库
2、重新编译nginx,添加http_geoip_module模块
检查原nginx配置参数
nginx源码目录下加上--with-http_geoip_module=dynamic参数重新配置
3、拷贝新编译的nginx和ngx_http_geoip_module.so到nginx安装目录下
4、配置规则
nginx.conf配置文件添加lngx_http_geoip_module.so模块
nginx.conf添加规则
站点配置文件server块中添加访问限制
本篇文章环境:系统centos7,nginx版本1.18.0
1、安装geoip库
#安装完成后,geip数据在/usr/share/GeoIP/目录下
yum -y install geoip-devel
2、重新编译nginx,添加http_geoip_module模块
检查原nginx配置参数
/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
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/local/nginx --with-http_ssl_module --with-http_v2_module
nginx源码目录下加上--with-http_geoip_module=dynamic参数重新配置
cd /usr/local/src/nginx-1.18.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_geoip_module=dynamic
make,注意不要make install,加上make install会覆盖现有nginx目录
make
3、拷贝新编译的nginx和ngx_http_geoip_module.so到nginx安装目录下
#备份,防止出错
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp /usr/local/src/nginx-1.18.0/objs/nginx /usr/local/nginx/sbin/
mkdir /usr/local/nginx/modules
cp /usr/local/src/nginx-1.18.0/objs/ngx_http_geoip_module.so /usr/local/nginx/modules/
4、配置规则
nginx.conf配置文件添加lngx_http_geoip_module.so模块
load_module modules/ngx_http_geoip_module.so;
nginx.conf添加规则
# 访问地理位置限制规则
geoip_country /usr/share/GeoIP/GeoIP.dat;
# geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
# 下面一行根据实际情况编写
map $geoip_country_code $allowed_country {
default no;
CN yes;
}
站点配置文件server块中添加访问限制
server {
listen 80;
server_name xx.xx.xx;
#添加判断
if ($allowed_country = no) {
return 403;
}
}
很赞哦! ()
相关文章
随机图文
-
pve添加新硬盘---U盘安装Proxmox VE(二)
上一篇《U盘安装Proxmox VE(一)》制作好启动盘后,插入U盘,设置bios从U盘启动,开始安装pve。选择Install Proxmox VE后,不再报错no cdrom found,但是自检过后出现白屏,鼠标可以正常移动,鼠标右击有菜单,就是看不到agree选项, -
centos7 手动编译安装 Nginx 1.18.0
工作中经常使用到nginx,本篇记录一下手工编译安装nginx过程 -
Centos7 mysql5.7.25 tar包解压安装
1、mysql官网下载mysql-5.7.25-el7-x86_64.tar.gz[root@centos7 src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-el7-x86_64.tar.gz 2、卸载系统 -
一个好用的linux rpm包搜索网站
我们在配置运行环境的时候经常遇到缺少这个那个包,推荐一个最全的rpm包搜索网站。使用方法1:直接搜索、下载使用 使用方法2:添加至linux服务器的源