nginx 模块支持IPv6
This commit is contained in:
parent
7d6ea00303
commit
007cb0808a
|
|
@ -0,0 +1,37 @@
|
||||||
|
ARG NGINX_VERSION=1.29.6
|
||||||
|
FROM nginx:${NGINX_VERSION} AS build
|
||||||
|
ARG NGINX_VERSION
|
||||||
|
|
||||||
|
# prepare the build environment
|
||||||
|
RUN apt-get update && \
|
||||||
|
apt-get install -y build-essential libpcre2-dev zlib1g-dev libssl-dev git
|
||||||
|
|
||||||
|
WORKDIR /usr/src
|
||||||
|
RUN curl -LO https://github.com/nginx/nginx/releases/download/release-${NGINX_VERSION}/nginx-${NGINX_VERSION}.tar.gz && \
|
||||||
|
tar -zxf nginx-$NGINX_VERSION.tar.gz
|
||||||
|
|
||||||
|
COPY . /usr/src/ip2region
|
||||||
|
|
||||||
|
WORKDIR /usr/src/ip2region/binding/c
|
||||||
|
RUN make xdb_searcher_lib
|
||||||
|
|
||||||
|
# parameters for building dynamic modules
|
||||||
|
WORKDIR /usr/src
|
||||||
|
RUN nginx -V 2>&1 | grep 'configure arguments' | sed 's/ --/ \\\n --/g' | sed "s/pie'/pie' \\\/g" | grep -v 'configure arguments' >> /tmp/conf_arg
|
||||||
|
|
||||||
|
RUN 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
|
||||||
|
RUN cat /tmp/conf_arg
|
||||||
|
|
||||||
|
WORKDIR /usr/src/nginx-$NGINX_VERSION
|
||||||
|
RUN eval "./configure $(cat /tmp/conf_arg)"
|
||||||
|
|
||||||
|
RUN make modules && \
|
||||||
|
cp objs/ngx_http_ip2region_module.so /etc/nginx/modules
|
||||||
|
|
||||||
|
# for buildx export
|
||||||
|
FROM scratch AS export_so
|
||||||
|
ARG NGINX_VERSION
|
||||||
|
COPY --from=build /etc/nginx/modules/ngx_http_ip2region_module.so /ngx_http_ip2region_module.so
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
:globe_with_meridians: [中文简体](README_zh.md) | [English](README.md)
|
||||||
|
|
||||||
# nginx-ip2region
|
# nginx-ip2region
|
||||||
|
|
||||||
## build
|
## build
|
||||||
|
|
@ -5,18 +7,16 @@
|
||||||
```shell
|
```shell
|
||||||
$ mkdir -p workspace
|
$ mkdir -p workspace
|
||||||
$ cd workspace
|
$ cd workspace
|
||||||
$ wget https://nginx.org/download/nginx-1.23.4.tar.gz
|
$ wget https://nginx.org/download/nginx-1.23.6.tar.gz
|
||||||
$ tar -zxf nginx-1.23.4.tar.gz && rm -rf nginx-1.23.4.tar.gz
|
$ tar -zxf nginx-1.23.6.tar.gz && rm -rf nginx-1.23.6.tar.gz
|
||||||
$ git clone https://github.com/lionsoul2014/ip2region.git
|
$ git clone https://github.com/lionsoul2014/ip2region.git
|
||||||
$ # please check to version v2.13.0 by running
|
|
||||||
$ git checkout v2.13.0
|
|
||||||
$ cd ip2region/binding/c
|
$ cd ip2region/binding/c
|
||||||
$ make xdb_searcher_lib
|
$ make xdb_searcher_lib
|
||||||
$ cd ../../../nginx-1.23.4
|
$ cd ../../../nginx-1.23.6
|
||||||
$ ./configure \
|
$ ./configure \
|
||||||
--add-module=$(PWD)/../ip2region/binding/nginx \
|
--add-module=$(PWD)/../ip2region/binding/nginx \
|
||||||
--with-cc-opt="-I $(PWD)/../ip2region/binding/c/build/include" \
|
--with-cc-opt="-I $(PWD)/../ip2region/binding/c/build/include" \
|
||||||
--with-ld-opt="-L$(PWD)/../ip2region/binding/c/build/lib"
|
--with-ld-opt="-L $(PWD)/../ip2region/binding/c/build/lib"
|
||||||
$ make
|
$ make
|
||||||
$ make install
|
$ make install
|
||||||
```
|
```
|
||||||
|
|
@ -69,3 +69,31 @@ nginx access log sample
|
||||||
{"remote_addr": "127.0.0.1", "region": "Reserved|Reserved|Reserved|0|0", "http_x_forwarded_for": ""}
|
{"remote_addr": "127.0.0.1", "region": "Reserved|Reserved|Reserved|0|0", "http_x_forwarded_for": ""}
|
||||||
{"remote_addr": "127.0.0.1", "region": "Reserved|Reserved|Reserved|0|0", "http_x_forwarded_for": ""}
|
{"remote_addr": "127.0.0.1", "region": "Reserved|Reserved|Reserved|0|0", "http_x_forwarded_for": ""}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Additionally, you can build the nginx dynamic module using the Dockerfile in the current directory.
|
||||||
|
|
||||||
|
> * The [buildx](https://github.com/docker/buildx) plugin is required to enable export functionality.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
|
||||||
|
docker build -t export_so -o type=tar,dest=./so.tar .
|
||||||
|
# The final result is a dynamic module named ngx_http_ip2region_module.so.
|
||||||
|
tar xf so.tar && rm so.tar
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
usage of dynamic modules
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
# nginx.conf
|
||||||
|
load_module /etc/nginx/my-modules/ngx_http_ip2region_module.so;
|
||||||
|
|
||||||
|
http {
|
||||||
|
# ...
|
||||||
|
ip2region_db /etc/nginx/conf.d/ip2region_v4.xdb content;
|
||||||
|
ip2region_db6 /etc/nginx/conf.d/ip2region_v6.xdb content;
|
||||||
|
# ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,99 @@
|
||||||
|
:globe_with_meridians: [中文简体](README_zh.md) | [English](README.md)
|
||||||
|
|
||||||
|
# nginx-ip2region
|
||||||
|
|
||||||
|
## build
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ mkdir -p workspace
|
||||||
|
$ cd workspace
|
||||||
|
$ wget https://nginx.org/download/nginx-1.23.6.tar.gz
|
||||||
|
$ tar -zxf nginx-1.23.6.tar.gz && rm -rf nginx-1.23.6.tar.gz
|
||||||
|
$ git clone https://github.com/lionsoul2014/ip2region.git
|
||||||
|
$ cd ip2region/binding/c
|
||||||
|
$ make xdb_searcher_lib
|
||||||
|
$ cd ../../../nginx-1.23.6
|
||||||
|
$ ./configure \
|
||||||
|
--add-module=$(PWD)/../ip2region/binding/nginx \
|
||||||
|
--with-cc-opt="-I $(PWD)/../ip2region/binding/c/build/include" \
|
||||||
|
--with-ld-opt="-L$(PWD)/../ip2region/binding/c/build/lib"
|
||||||
|
$ make
|
||||||
|
$ make install
|
||||||
|
```
|
||||||
|
|
||||||
|
## nginx conf
|
||||||
|
|
||||||
|
> Syntax: `ip2region_db xdb_file_path [cache_policy Optional]`;
|
||||||
|
> Context: http
|
||||||
|
|
||||||
|
cache_policy: `file` or `vectorIndex` or `content`, default: `content`
|
||||||
|
|
||||||
|
Edit `nginx.conf` add `ip2region_db` directive
|
||||||
|
|
||||||
|
```nginx
|
||||||
|
...
|
||||||
|
http {
|
||||||
|
|
||||||
|
log_format main escape=json '{'
|
||||||
|
'"remote_addr": "$remote_addr", '
|
||||||
|
'"region": "$ip2region", '
|
||||||
|
'"http_x_forwarded_for": "$http_x_forwarded_for"'
|
||||||
|
'}';
|
||||||
|
|
||||||
|
access_log logs/access.log main;
|
||||||
|
|
||||||
|
# set xdb file path
|
||||||
|
ip2region_db ip2region.xdb;
|
||||||
|
# ip2region_db ip2region.xdb vectorIndex;
|
||||||
|
# ip2region_db ip2region.xdb file;
|
||||||
|
# ip2region_db ip2region.xdb content;
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name localhost;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
root html;
|
||||||
|
index index.html index.htm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Copy `ip2region_v4.xdb` to `nginx/config` folder (rename name it to ip2region.xdb), then restart nginx, the `region` data stored in `ip2region` variable
|
||||||
|
|
||||||
|
nginx access log sample
|
||||||
|
|
||||||
|
```log
|
||||||
|
{"remote_addr": "127.0.0.1", "region": "Reserved|Reserved|Reserved|0|0", "http_x_forwarded_for": ""}
|
||||||
|
{"remote_addr": "127.0.0.1", "region": "Reserved|Reserved|Reserved|0|0", "http_x_forwarded_for": ""}
|
||||||
|
```
|
||||||
|
|
||||||
|
另外,也可以使用当前路径的 Dockerfile 构建 nginx 动态模块
|
||||||
|
|
||||||
|
> *需要安装 [buildx](https://github.com/docker/buildx) 插件以支持导出*
|
||||||
|
|
||||||
|
```shell
|
||||||
|
|
||||||
|
docker build -t export_so -o type=tar,dest=./so.tar .
|
||||||
|
# 最终得到一个叫 ngx_http_ip2region_module.so 的动态模块
|
||||||
|
tar xf so.tar && rm so.tar
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
动态模块使用方式
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
# nginx.conf
|
||||||
|
load_module /etc/nginx/my-modules/ngx_http_ip2region_module.so;
|
||||||
|
|
||||||
|
http {
|
||||||
|
# ...
|
||||||
|
ip2region_db /etc/nginx/conf.d/ip2region_v4.xdb content;
|
||||||
|
ip2region_db6 /etc/nginx/conf.d/ip2region_v6.xdb content;
|
||||||
|
# ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Loading…
Reference in New Issue