nginx + geoip2简明配置

简介: 有时候需要nginx结合IP地址库做一些事情,比如过滤掉某个国家的IP,该如何做呢?

nginx结合GeoIP可以做一些很有趣的事情,比如过滤掉某些国家的IP。该如何配置呢?

安装环境

CentOS 7.9.2009

nginx 1.25.2

ngx_http_geoip2_module 3.4

libmaxminddb 1.7.1

注册maxmind帐号并下载GeoLite库

https://www.maxmind.com 官网注册个帐号,然后下载GeoLite2 City和GeoLite2 Country这两个IP地址库文件。下载后放在服务器的/usr/local/geoip目录下。

编译libmaxminddb

git clone https://github.com/maxmind/libmaxminddb.git -b1.7.1
cd libmaxminddb
# make编译./configure
makemake install
# 或者也可以使用cmake编译mkdir build && cd build
cmake ..
cmake --build .
cmake --build . --target install

编译nginx

git clone https://github.com/leev/ngx_http_geoip2_module.git
wget https://nginx.org/download/nginx-1.25.2.tar.gz
tar zxvf nginx-1.25.2.tar.gz
cd nginx-1.25.2
CFLAGS=-Wno-stringop-truncation ./configure \
--prefix=/usr/local/nginx \
--with-file-aio \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-http_secure_link_module \
--with-http_random_index_module \
--with-http_stub_status_module \
--with-http_v2_module \
--add-module=../ngx_http_geoip2_module \
--with-http_v2_modulemakemake install

安装geoipupdate

yum install libmaxminddb-devel geoipupdate

配置geoip

maxmind geoip配置文件保存在/etc/GeoIP.conf中,主要内容如下(需要修改的部分已加注释):

# cat GeoIP.conf# Please see http://dev.maxmind.com/geoip/geoipupdate/ for instructions# on setting up geoipupdate, including information on how to download a# pre-filled GeoIP.conf file.# Enter your account ID and license key below. These are available from# https://www.maxmind.com/en/my_license_key. If you are only using free# GeoLite databases, you may leave the 0 values.# 注册maxmind帐号后可获取AccountID xxxxxxxxxx
LicenseKey xxxxxxxxx
# Enter the edition IDs of the databases you would like to update.# Multiple edition IDs are separated by spaces.EditionIDs GeoLite2-Country GeoLite2-City
# The remaining settings are OPTIONAL.# The directory to store the database files. Defaults to /usr/local/share/GeoIP# geoip数据库位置DatabaseDirectory /usr/local/geoip
# The server to use. Defaults to "updates.maxmind.com".# Host updates.maxmind.com# The desired protocol either "https" (default) or "http".# Protocol https# The proxy host name or IP address. You may optionally specify a# port number, e.g., 127.0.0.1:8888. If no port number is specified, 1080# will be used.# Proxy 127.0.0.1:8888# The user name and password to use with your proxy server.# ProxyUserPassword username:password# Whether to skip host name verification on HTTPS connections.# Defaults to "0".# SkipHostnameVerification 0# Whether to skip peer verification on HTTPS connections.# Defaults to "0".# SkipPeerVerification 0# Whether to preserve modification times of files downloaded from the server.# Defaults to "0".# PreserveFileTimes 0# The lock file to use. This ensures only one geoipupdate process can run at a# time.# Defaults to ".geoipupdate.lock" under the DatabaseDirectory.# LockFile /usr/local/share/GeoIP/.geoipupdate.lock

nginx相关配置

    geoip2 /usr/local/geoip/GeoLite2-City.mmdb {
#default是为了当ip查不到信息时设置一个默认值。$countrydefault=CN country iso_code;
$city_namedefault=Beijing city names en;
$provincedefault=Henan subdivisions 0 names en;
    }
    map $province$allowed_province {
        default yes;
#Guangdong no;#Guangxi no;#Fujian no;#Beijing no;#Shanghai no;#Tianjin no;    }
    map $country$allowed_country {
        default yes;  #默认都拒绝        CN yes;  #中国大陆        TW yes;  #中国台湾        HK yes;  #中国香港        MO yes;  #中国澳门        TH yes;  #泰国        VN yes;  #越南        US no;   #美国        BR no;
        IE yes;
        KOR yes;
    }

GeoIP日常使用及更新

# 查看IP地址属地mmdblookup --file /usr/local/geoip/GeoLite2-City.mmdb --ip223.104.30.232 subdivisions 0 names
# 更新IP地址库,因为IP地址库更新比较频繁,建议加入crontab中geoipupdate
相关文章
|
3月前
|
编解码 应用服务中间件 Linux
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
365 1
|
7月前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
749 87
|
7月前
|
负载均衡 应用服务中间件 nginx
Nginx配置与命令
Nginx 是一款高性能的 HTTP 和反向代理服务器,其配置文件灵活且功能强大。本文介绍了 Nginx 配置的基础结构和常用指令,包括全局块、Events 块、HTTP 块及 Server 块的配置方法,以及静态资源服务、反向代理、负载均衡、HTTPS 和 URL 重写等功能实现。此外,还提供了常用的 Nginx 命令操作,如启动、停止、重载配置和日志管理等,帮助用户高效管理和优化服务器性能。
|
3月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
283 18
|
3月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
367 17
|
4月前
|
数据建模 应用服务中间件 PHP
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
本示例演示如何通过Docker挂载同一宿主目录至Nginx与PHP容器,实现PHP项目运行环境配置。需注意PHP容器中监听地址修改为0.0.0.0:9000,并调整Nginx配置中fastcgi_pass指向正确的IP与端口。同时确保Nginx容器中/var/www/html权限正确,以避免访问问题。
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
|
5月前
|
应用服务中间件 网络安全 nginx
配置Nginx以支持Websocket连接的方法。
通过上述配置,Nginx将能够理解WebSocket协议的特殊要求,代理Websocket流量到合适的后端服务器。注意,Websocket并不是HTTP,尽管它最初是通过HTTP请求启动的连接升级,因此保证Nginx了解并能够妥善处理这种升级流程是关键。
1215 10
|
4月前
|
Ubuntu 应用服务中间件 Linux
在Ubuntu上配置Nginx实现开机自启功能
至此,Nginx应该已经被正确地设置为开机自启。在Ubuntu中利用 `systemd`对服务进行管理是一种高效的方式,为系统管理员提供了强大的服务管理能力,包括但不限于启动、停止、重启服务,以及配置服务的开机自启动。通过这些简洁的命令,即使是对Linux不太熟悉的用户也能轻松地进行配置。
202 0
|
6月前
|
安全 应用服务中间件 网络安全
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
412 0
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
|
6月前
|
JSON 前端开发 应用服务中间件
配置Nginx根据IP地址进行流量限制以及返回JSON格式数据的方案
最后,记得在任何生产环境部署之前,进行透彻测试以确保一切运转如预期。遵循这些战术,守卫你的网络城堡不再是难题。
276 3