ngx_stream_geoip2_module 怎么部署
时间: 2025-06-28 16:01:10 浏览: 14
### 配置 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]。
阅读全文
相关推荐












