Skip to content

Commit 6176184

Browse files
committed
support geoip2
1 parent b34b580 commit 6176184

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

Dockerfile

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1+
FROM nginxproxy/nginx-proxy AS builder
2+
3+
ENV GEOIP2_VERSION=3.4
4+
5+
# 安装编译依赖并编译 GeoIP2 模块
6+
RUN apt-get update && apt-get install -y --no-install-recommends \
7+
build-essential \
8+
libpcre3-dev \
9+
zlib1g-dev \
10+
libgeoip-dev \
11+
libmaxminddb-dev \
12+
wget \
13+
git \
14+
&& cd /opt \
15+
&& git clone --depth 1 -b $GEOIP2_VERSION --single-branch https://github.com/leev/ngx_http_geoip2_module.git \
16+
&& wget -O - http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz | tar xzf - \
17+
&& cd nginx-$NGINX_VERSION \
18+
&& ./configure --with-compat --add-dynamic-module=/opt/ngx_http_geoip2_module \
19+
&& make modules
20+
21+
# 最终镜像
122
FROM nginxproxy/nginx-proxy
2-
RUN { \
3-
echo 'server_tokens off;'; \
4-
echo 'client_max_body_size 1024m;'; \
5-
echo 'proxy_buffer_size 256k;'; \
6-
echo 'proxy_buffers 4 256k;'; \
7-
echo 'proxy_busy_buffers_size 256k;'; \
8-
echo 'proxy_hide_header X-Generator;'; \
9-
echo 'client_header_buffer_size 64k;'; \
10-
echo 'large_client_header_buffers 4 64k;'; \
11-
echo 'proxy_connect_timeout 300;'; \
12-
echo 'proxy_send_timeout 300;'; \
13-
echo 'proxy_read_timeout 300;'; \
14-
echo 'send_timeout 300;'; \
15-
} > /etc/nginx/conf.d/my_proxy.conf
23+
24+
# 复制编译好的模块
25+
COPY --from=builder /opt/nginx-*/objs/ngx_http_geoip2_module.so /usr/lib/nginx/modules/
26+
27+
# 安装运行时依赖并下载 GeoIP 数据库
28+
RUN apt-get update \
29+
&& apt-get install -y --no-install-recommends libmaxminddb0 wget \
30+
&& apt-get clean \
31+
&& rm -rf /var/lib/apt/lists/* \
32+
&& chmod 644 /usr/lib/nginx/modules/ngx_http_geoip2_module.so \
33+
&& sed -i '1iload_module /usr/lib/nginx/modules/ngx_http_geoip2_module.so;' /etc/nginx/nginx.conf \
34+
&& mkdir -p /usr/share/geoip \
35+
&& wget -O /usr/share/geoip/GeoLite2-Country.mmdb https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-Country.mmdb \
36+
&& wget -O /usr/share/geoip/GeoLite2-City.mmdb https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-City.mmdb
37+
38+
COPY rootfs/etc/nginx/conf.d/my_proxy.conf /etc/nginx/conf.d/my_proxy.conf
39+
COPY rootfs/etc/nginx/conf.d/geoip2.conf /etc/nginx/conf.d/geoip2.conf
1640
ADD https://raw.githubusercontent.com/h5bp/server-configs-nginx/main/h5bp/web_performance/compression.conf /etc/nginx/conf.d/compression-gzip.conf
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
set_real_ip_from 0.0.0.0/0;
2+
real_ip_header X-Forwarded-For;
3+
real_ip_recursive on;
4+
5+
map $http_x_forwarded_for $realip {
6+
~^(\d+\.\d+\.\d+\.\d+) $1;
7+
default $remote_addr;
8+
}
9+
10+
geoip2 /usr/share/geoip/GeoLite2-Country.mmdb {
11+
auto_reload 24h;
12+
$geoip2_metadata_country_build metadata build_epoch;
13+
$geoip2_data_country_code source=$realip country iso_code;
14+
$geoip2_data_country_name source=$realip country names en;
15+
}
16+
geoip2 /usr/share/geoip/GeoLite2-City.mmdb {
17+
auto_reload 60m;
18+
$geoip2_metadata_city_build metadata build_epoch;
19+
$geoip2_data_city source=$realip city names en;
20+
$geoip2_data_latitude source=$realip location latitude;
21+
$geoip2_data_longitude source=$realip location longitude;
22+
$geoip2_data_time_zone source=$realip location time_zone;
23+
$geoip2_data_region source=$realip subdivisions iso_code;
24+
$geoip2_data_region_name source=$realip subdivisions names en;
25+
$geoip2_data_country_code source=$realip country iso_code;
26+
$geoip2_data_country_name source=$realip country names en;
27+
$geoip2_data_continent_code source=$realip continent code;
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
server_tokens off;
2+
client_max_body_size 1024m;
3+
proxy_buffer_size 256k;
4+
proxy_buffers 4 256k;
5+
proxy_busy_buffers_size 256k;
6+
proxy_hide_header X-Generator;
7+
client_header_buffer_size 64k;
8+
large_client_header_buffers 4 64k;
9+
proxy_connect_timeout 300;
10+
proxy_send_timeout 300;
11+
proxy_read_timeout 300;
12+
send_timeout 300;

0 commit comments

Comments
 (0)