Nginx高级之高级模块(secure_link/secure_link_md5/geoip_module)

本文深入探讨了Nginx的secure_link和secure_link_md5模块,通过实例展示了如何生成和验证链接密钥,以保护资源免受未经授权的访问,并介绍了如何利用geoip模块进行IP地理位置识别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

高级阶段回顾:

Nginx高级之Rewrite规则

secure_link/secure_link_md5模块

制定并允许检查请求的链接的真实性以保护资源免遭未授权的访问 / 限制链接生效的周期

安全模块应用场景模型

模块配置语法

systax: secure_link [表达式]

default: 无配置

context: http域/server域/location域

md5加密:

systax: secure_link_md5 [表达式]

default: 无配置

context: http域/server域/location域

下面做一个小测试

① 进入default.conf配置:

② 在其他目录下建立一个.sh文件, 内容如下(根据自己环境情况进行相应修改即可):

servername="192.168.19.136"
download_file="/download/testMD5.img"
time_num=$(date -d "2019-11-26 00:00:00" +%s)
secret_num="Milky-Way"

res=$(echo -n "${time_num}${download_file} ${secret_num}"|openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)

echo "http://${servername}${download_file}?md5=${res}&expires=${time_num}"

③ 这个生成密钥的方式一般由后台生成, 这里采用自己写的这个.sh文件来生成密钥, 命令: sh [.sh文件的名称], 我这里是sh md5url.sh

生成的密钥: http://192.168.19.136/download/testMD5.img?md5=6sL9aEi50TlupLSLtX7jcA&expires=1574697600

④ 根据这个密钥来访问服务器

结果如下:

geoip模块

基于maxmind geoip的二进制文件, 读取ip所在地信息

需要安装geoip源: yum install nginx-module-geoip

查看是否安装完成:

使用场景

① 区别国内外的ip

② 区别城市间的ip

配置

安装yum源后, 进入nginx.conf, 手动引入geoip模块:

在配置default.conf之前需要先下载ip地域的文件, 利用.sh文件来下载, 在任意地方新建一个.sh文件, 内容如下:

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

在目录下会生成2个.gz的文件, 我把它们拷贝至/etc/nginx/geoip目录下解压生成2个.dat文件

接下来进入default.conf配置: 在http域引入上面生成的2个.dat文件

在代理的情况下($geoip_country_code不为CN)访问nginx, 结果如下:

在无代理的情况下用本地ip($geoip_country_code为CN)访问nginx, 结果如下:

 

### 配置 Nginx 使用 `ngx_stream_geoip2_module` 模块 #### 安装依赖项 为了成功编译和运行 `ngx_stream_geoip2_module`,需要先安装必要的开发工具和库文件。这通常包括 MaxMind 的数据库支持库。 ```bash yum install libmaxminddb-devel -y ``` #### 下载并准备模块源码 获取最新的 `ngx_http_geoip2_module` 和 `ngx_stream_geoip2_module` 源代码: ```bash cd /usr/local/src/ git clone https://github.com/leev/ngx_http_geoip2_module.git ``` 注意:虽然这里提到的是 `ngx_http_geoip2_module`,但是 GitHub 上该项目也包含了流模式的支持即 `ngx_stream_geoip2_module`. #### 编译安装 NGINX 并添加自定义模块 在编译 NGINX 之前,确保已经下载了官方发布的稳定版本,并将其解压缩到合适的位置。接着,在配置阶段通过命令行参数指定要加入的新功能模块路径。 ```bash tar -zxvf nginx-1.20.1.tar.gz cd nginx-1.20.1/ ./configure \ --with-stream \ --add-dynamic-module=../ngx_http_geoip2_module \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx \ --group=nginx \ --with-compat \ --with-file-aio \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_xslt_module=dynamic \ --with-http_image_filter_module=dynamic \ --with-http_geoip_module=dynamic \ --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_degradation_module \ --with-http_slice_module \ --with-http_stub_status_module \ --with-mail=static \ --with-mail_ssl_module \ --with-pcre \ --with-pcre-jit \ --with-stream=dynamic \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_geoip_module=dynamic \ --with-stream_ssl_preread_module \ --with-debug make && make install ``` 上述过程会将 `ngx_stream_geoip2_module` 动态加载至 NGINX 中[^3]。 #### 获取 IP 地理位置数据集 (MaxMind DB 文件) 从官方网站或其他可信资源处获得最新版的 GeoLite2 数据包,并按照说明放置于适当位置供 NGINX 访问。 ```bash mkdir -p /etc/nginx/geoip cd /tmp wget https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=YOUR_LICENSE_KEY&suffix=tar.gz tar xf geoip_download\?edition_id\=GeoLite2-City* -C /etc/nginx/geoip --strip-components=1 ``` 请注意替换 URL 参数中的 `YOUR_LICENSE_KEY` 为你自己的许可证密钥[^2]。 #### 修改 NGINX 配置以启用 Stream GeoIP 支持 编辑 `/etc/nginx/nginx.conf` 或者相应的虚拟主机配置文件来引入新的指令用于处理 TCP/UDP 请求的同时解析客户端的真实地理位置信息。 ```nginx stream { upstream backend { server 192.168.1.1:80; } map $remote_addr $country_code { default "unknown"; include /etc/nginx/geoip/country.map; # 自定义映射表可选 } log_format stream_combined '$remote_addr [$time_local] ' '"$protocol $status" $bytes_sent "$upstream_addr"' ' country=$country_code'; access_log /var/log/nginx/stream-access.log stream_combined; server { listen 12345; proxy_pass backend; geoip2 /etc/nginx/geoip/GeoLite2-City.mmdb { auto_load on; $city city.names.en; $country_code country.iso_code; } set $allowed_countries "CN US CA"; # 允许访问的国家列表 if ($country_code !~* ^($allowed_countries)$ ) { return 503; } } } ``` 此段配置实现了基于地理区域限制服务可用性的基本逻辑,仅当请求来自特定几个允许的国家时才转发流量给后端服务器;否则返回 HTTP 503 错误响应表示暂时不可用[^4]。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值