基于docker 镜像的 nginx 编译添加第三方模块

镜像

镜像就用 官方镜像 alpine 版本,
https://hub.docker.com/_/nginx/tags

此示例中 使用 docker pull nginx:1.23.3-alpine

编译环境

首先启动个容器,用于编辑环境,之后把编译好的 nginx 可执行程序拷贝出来。

docker run -it --name nginx-build nginx:1.23.3-alpine sh
apk add --no-cache --virtual .build-deps \
        gcc \
        libc-dev \
        make \
        openssl-dev \
        pcre-dev \
        zlib-dev \
        linux-headers \
        curl \
        gnupg \
        libxslt-dev \
        gd-dev \
        geoip-dev 

第三方模块

主动对后端服务器进行状态检查

目前由于 淘宝主动后端检查版本和最新的 1.20.2 版本兼容(2023.01.05)
所有需要下载的 nginx 源码包是 nginx-1.20.2

curl -Lo nginx_upstream_check_module-master.tar.gz https://gitcode.net/mirrors/yaoweibin/nginx_upstream_check_module/-/archive/master/nginx_upstream_check_module-master.tar.gz

需要进入 nginx-1.20.2 的源码目录打个补丁, 注意这里需要将 nginx-1.20.2 的源码包拷贝到 我们上一不创建的环境中再执行打补丁,因为需要在兼容的环境总大打补丁。
打补丁命令: patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch

编译的时候增加如下参数
--add-module=/path/to/nginx_upstream_check_module-master

配置指南: http://tengine.taobao.org/document_cn/http_upstream_check_cn.html

内容替换

在响应正文中使用正则表达式和固定字符串进行文本替换。

一个第三方的替换模块:ngx_http_substitutions_filter_module,来实现我们的各种需求。

经过测试,这个模块至少有如下实用功能:
①、支持多次替换
②、支持正则替换
③、支持中文替换
Ps:略有遗憾的是,这个替换不能使用到 if 判断模块内,否则就超神了。。。

github:https://github.com/yaoweibin/ngx_http_substitutions_filter_module/

配置块: context: http, server, location

如果支持正则,需要在 末尾处添加 r 修饰符。
示例:

    location / {
        #subs_filter_types *;
        subs_filter_types text/html text/css text/xml;
        subs_filter st(\d*).example.com $1.example.com ir;
        subs_filter a.example.com s.example.com;
        subs_filter http://$host https://$host;
    }
  • r 正则
  • i 忽略大小写
  • o 只需替换第一个
curl -Lo substitutions_filter.zip https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/refs/heads/master.
zip

编译的时候增加如下参数
--add-module=/path/to/ngx_http_substitutions_filter_module-master

解码

对后端返回的 url 进行解码
https://github.com/openresty/set-misc-nginx-module#installation
这个模块是 openresty 的,还有依赖的模块 https://codeload.github.com/vision5/ngx_devel_kit/zip/refs/tags/v0.3.2

 curl -Lo set-misc-nginx.tar.gz https://github.com/openresty/set-misc-nginx-module/archive/refs/tags/v0.33.tar.gz
 curl -Lo ngx_devel_kit.zip https://codeload.github.com/vision5/ngx_devel_kit/zip/refs/tags/v0.3.2

编译的时候增加如下参数
--add-module=/path/to/ngx_devel_kit-0.3.2 --add-module=/path/to/set-misc-nginx-module-0.33

开始构建

1 将这些需要的第三方模块拷贝到编译环境容器 nginx-build 中的 root 目录下

docker  cp  模块目录   nginx-build:/root/

2 将 nginx-12.0.2 的源码也拷贝到编译环境容器 nginx-build 中的 root 目录下

wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar -xf   nginx-1.20.2.tar.gz
docerk cp nginx-1.20.2 nginx-build:/root/

3 打补丁
进入nginx源码包目录执行

patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch

4 在编译环境容器 nginx-build 中获取到原来的编译参数

nginx  -V

5 将获取到的原编译参数和模块的编译参数进行组合

./configure --prefix=/etc/nginx   --add-module=../nginx_upstream_check_module-master --add-module=../ngx_http_subst
itutions_filter_module-master --add-module=../ngx_devel_kit-0.3.2 --add-module=../set-misc-nginx-module-0.33  后面跟获取到的原来配置参数

6 执行 makels
7 将编译好的可执行文件 objs/nginx 拷贝出来
8 将拷贝出来的 nginx 文件拷贝到一个新的容器内,并构建新的容器
dockerfile

FROM  nginx:1.23.3-alpine
LABEL description="根据自己构建的情况,编写描述信息"
COPY ./nginx /usr/sbin/nginx
RUN apk add --no-cache --virtual .build-deps \
    openssl-dev \
    pcre-dev \
    zlib-dev \
    linux-headers \
    libxslt-dev \
    gnupg \
    gd-dev geoip-dev
docker build -t nginx:1.20.2-alpine-upstream .

使用 openresty 替代nginx

https://hub.docker.com/r/openresty/openresty

下载镜像

https://hub.docker.com/_/nginx/tags

docker run -v /my/custom/conf.d:/etc/nginx/conf.d openresty/openresty:alpine

docker pull openresty/openresty:1.21.4.1-4-alpine-amd64
docker pull openresty/openresty:1.21.4.1-alpine-aarch64
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shark_西瓜甜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值