diff --git a/binding/nginx/Dockerfile b/binding/nginx/Dockerfile new file mode 100644 index 0000000..0f65eae --- /dev/null +++ b/binding/nginx/Dockerfile @@ -0,0 +1,37 @@ +ARG NGINX_VERSION=1.27.0 +FROM nginx:${NGINX_VERSION} AS build + +# 嫌更新慢,可以试试这个镜像站地址 +# sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \ +RUN apt-get update && \ + apt-get install -y build-essential libpcre3-dev zlib1g-dev libssl-dev git + +WORKDIR /usr/src +RUN curl -O http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \ + tar -zxf nginx-${NGINX_VERSION}.tar.gz && \ + git clone --depth=1 https://github.com/lionsoul2014/ip2region.git && \ + # 生成configure参数, 最好与nginx原始配置参数一致, 不然模块会出现兼容问题 \ + nginx -V 2>&1 | grep 'configure arguments' | sed 's/ --/ \\\n --/g' | sed "s/pie'/pie' \\\/g" | grep -v 'configure arguments' >> /tmp/conf_arg && \ + echo \ + ' --add-dynamic-module=$(pwd)/../ip2region/binding/nginx \\\n' \ + ' --with-cc-opt="-I $(pwd)/../ip2region/binding/c/build/include" \\\n' \ + ' --with-ld-opt="-L $(pwd)/../ip2region/binding/c/build/lib"' >> /tmp/conf_arg + +WORKDIR /usr/src/ip2region/binding/c +RUN make xdb_searcher_lib + +WORKDIR /usr/src/nginx-${NGINX_VERSION} +RUN cat /tmp/conf_arg && \ + bash -c 'eval "./configure $(cat /tmp/conf_arg)"' && \ + make modules && \ + cp /usr/src/nginx-${NGINX_VERSION}/objs/ngx_http_ip2region_module.so / + +# 用于builx导出 +FROM scratch AS export_so +COPY --from=build /ngx_http_ip2region_module.so / + +# 第三阶段,只把编译出来兼容版本的模块添加进自定义 nginx 镜像,节省空间 +FROM nginx:${NGINX_VERSION} +COPY --from=build /usr/src/nginx-${NGINX_VERSION}/objs/ngx_http_ip2region_module.so /etc/nginx/modules/ + +CMD ["nginx", "-g", "daemon off;"] diff --git a/binding/nginx/README.md b/binding/nginx/README.md index e71f9c0..9361557 100644 --- a/binding/nginx/README.md +++ b/binding/nginx/README.md @@ -19,6 +19,25 @@ $ make $ make install ``` +## 利用 docker 构建动态模块 + +```shell +# 启用 BuildKit +export DOCKER_BUILDKIT=1 + +# 构建一个包含ip2region的动态模块自定义镜像, 默认版本由参数 `ARG NGINX_VERSION=1.27.0` 控制 +docker build -t custom-nginx . + +# 构建一个包含ip2region的动态模块自定义镜像, 且指定nginx版本 +docker build -t custom-nginx:1.26.1 --build-arg NGINX_VERSION=1.26.1 . + +# 导出动态模块到当前目录, target表示目标阶段, o是output 输出类型和位置 +docker build --target export_so -o type=tar,dest=./so.tar . +# 解压即可看到 `ngx_http_ip2region_module.so`, 可将此模块放置到nginx的模块目录,比如 /etc/nginx/modules/ +tar xf so.tar + +``` + ## nginx conf > Syntax: `ip2region_db xdb_file_path [cache_policy Optional]`; @@ -29,7 +48,12 @@ cache_policy: `file` or `vectorIndex` or `content`, default: `content` Edit `nginx.conf` add `ip2region_db` directive ```nginx -... + +... + +# 如果是动态模块需要使用 load_module 的方式来加载它 +# load_module /etc/nginx/modules/ngx_http_ip2region_module.so; + http { log_format main escape=json '{' @@ -59,7 +83,7 @@ http { ``` -Copy `ip2region.xdb` to `nginx/config` folder, then restart nginx, the `region` data stored in `ip2region` variable +Copy `ip2region.xdb` to `nginx/config` (e.g. nginx/conf.d) folder, then restart nginx, the `region` data stored in `ip2region` variable nginx access log sample