openresty 添加 ngx_cache_purge 模块创建代理缓存 docker 容器
本文将从零创建一个 docker 容器的、集成第三方模块 ngx_cache_purge 的代理缓存服务器。
centos8 openresty/docker-openresty ngx_cache_purge
简述
https://github.com/openresty/docker-openresty 是官方库,有 centos7 centos8 版本的 Dockerfile,但官方并未集成 ngx_cache_purge,目前所知的方案只有编译安装,我们基于官方 Dockerfile 重写。
准备资源
鉴于 Dockerfile 中下载有时卡网络,官方下载地址 github.com/FRiCKLE/ngx_cache_purge
然后把 ngx_cache_purge-2.3.tar.gz 放入 Dockerfile 同级目录
[root@VM-20-3-opencloudos dockerfile]# ls
Dockerfile ngx_cache_purge-2.3.tar.gz
Dockerfile
该 Dockerfile 是本人借助 Deepseek 摸索出来的,目前已构筑容器完毕,代码开发中,目前没有问题,均正常使用。
该 Dockerfile 基于宿主机 centos8,如果你的宿主机是 centos7 那就 ARG RESTY_IMAGE_TAG 改成 7(我没有试),介绍将下方代码后展开
# Dockerfile - CentOS 8 - Source build with ngx_cache_purge
# Based on official OpenResty Dockerfile with ngx_cache_purge module
ARG RESTY_IMAGE_BASE="centos"
ARG RESTY_IMAGE_TAG="8"
FROM ${RESTY_IMAGE_BASE}:${RESTY_IMAGE_TAG}
COPY ngx_cache_purge-2.3.tar.gz /tmp/ngx_cache_purge-2.3.tar.gz
LABEL maintainer="Evan Wies <evan@neomantra.net>"
ARG RESTY_IMAGE_BASE="centos"
ARG RESTY_IMAGE_TAG="8"
ARG RESTY_LUAROCKS_VERSION="3.12.2"
ARG RESTY_VERSION="1.27.1.1"
ARG RESTY_J="1"
LABEL resty_image_base="${RESTY_IMAGE_BASE}"
LABEL resty_image_tag="${RESTY_IMAGE_TAG}"
LABEL resty_luarocks_version="${RESTY_LUAROCKS_VERSION}"
LABEL resty_config_options="${RESTY_CONFIG_OPTIONS}"
LABEL resty_j="${RESTY_J}"
# 配置参数
ARG RESTY_CONFIG_OPTIONS="\
--prefix=/usr/local/openresty \
--with-openssl=/tmp/openssl-3.0.15 \
--with-pcre-jit \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_v2_module \
--with-http_v3_module \
--with-ipv6 \
--with-md5-asm \
--with-sha1-asm \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_secure_link_module \
--with-http_random_index_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_slice_module \
--with-threads \
--with-compat \
--with-http_ssl_module \
"
# 设置镜像源
RUN find /etc/yum.repos.d -name 'CentOS-Linux-*' -exec sed -i 's/mirrorlist/#mirrorlist/g' {} \; \
&& find /etc/yum.repos.d -name 'CentOS-Linux-*' -exec sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://mirrors.aliyun.com|g' {} \; \
# 安装编译依赖
&& yum install -y \
gcc \
gcc-c++ \
gd-devel \
gettext \
git \
libxslt-devel \
make \
perl \
perl-ExtUtils-Embed \
readline-devel \
zlib-devel \
which \
openssl-devel \
pcre-devel \
wget \
bind-utils \
iproute \
crontabs \
epel-release \
jq \
tar \
unzip \
&& yum clean all \
&& rm -rf /var/cache/yum/*
RUN cd /tmp \
# 下载 OpenResty 源码
&& curl -fSL https://openresty.org/download/openresty-${RESTY_VERSION}.tar.gz -o openresty-${RESTY_VERSION}.tar.gz \
&& tar xzf openresty-${RESTY_VERSION}.tar.gz \
# 使用本地的 ngx_cache_purge 压缩包
&& tar -xzf ngx_cache_purge-2.3.tar.gz \
&& mv ngx_cache_purge-2.3 ngx_cache_purge \
# 下载并编译 OpenSSL 3.0
&& curl -fSL https://www.openssl.org/source/openssl-3.0.15.tar.gz -o openssl-3.0.15.tar.gz \
&& tar xzf openssl-3.0.15.tar.gz \
&& cd openssl-3.0.15 \
&& ./config --prefix=/usr/local/openssl-3.0.15 \
&& make -j${RESTY_J} \
&& make install \
# 编译 OpenResty 并添加 ngx_cache_purge 模块
&& cd /tmp/openresty-${RESTY_VERSION} \
&& ./configure -j${RESTY_J} ${RESTY_CONFIG_OPTIONS} --add-module=/tmp/ngx_cache_purge \
&& make -j${RESTY_J} \
&& make -j${RESTY_J} install \
&& cd /tmp \
&& rm -rf openresty-${RESTY_VERSION} openresty-${RESTY_VERSION}.tar.gz ngx_cache_purge ngx_cache_purge-2.3.tar.gz \
# 安装 LuaRocks
&& cd /tmp \
&& curl -fSL https://luarocks.github.io/luarocks/releases/luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz -o luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz \
&& tar xzf luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz \
&& cd luarocks-${RESTY_LUAROCKS_VERSION} \
&& ./configure \
--prefix=/usr/local/openresty/luajit \
--with-lua=/usr/local/openresty/luajit \
--with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1 \
&& make build \
&& make install \
&& cd /tmp \
&& rm -rf luarocks-${RESTY_LUAROCKS_VERSION} luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz \
# 创建必要的目录和符号链接
&& mkdir -p /var/run/openresty \
&& ln -sf /dev/stdout /usr/local/openresty/nginx/logs/access.log \
&& ln -sf /dev/stderr /usr/local/openresty/nginx/logs/error.log
# 添加 PATH
ENV PATH=$PATH:/usr/local/openresty/luajit/bin:/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin
# Lua 路径
ENV LUA_PATH="/usr/local/openresty/site/lualib/?.ljbc;/usr/local/openresty/site/lualib/?/init.ljbc;/usr/local/openresty/lualib/?.ljbc;/usr/local/openresty/lualib/?/init.ljbc;/usr/local/openresty/site/lualib/?.lua;/usr/local/openresty/site/lualib/?/init.lua;/usr/local/openresty/lualib/?.lua;/usr/local/openresty/lualib/?/init.lua;./?.lua;/usr/local/openresty/luajit/share/luajit-2.1/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/openresty/luajit/share/lua/5.1/?.lua;/usr/local/openresty/luajit/share/lua/5.1/?/init.lua"
ENV LUA_CPATH="/usr/local/openresty/site/lualib/?.so;/usr/local/openresty/lualib/?.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so"
CMD ["/usr/local/openresty/nginx/sbin/nginx", "-g", "daemon off;"]
STOPSIGNAL SIGQUIT
RESTY_VERSION="1.27.1.1" 实际就是 nginx 版本RESTY_CONFIG_OPTIONS 实际就是 nginx 的 configure 参数,配置项全部采用了官方文档里的所有配置项,去除了 email 相关的模块(用不到), 在官方 github.com/openresty/openresty 文档里能找到。
OpenSSL 用的是较新版本 v3.0.15
现在执行:
docker build -t resty1.27:proxy_cache .
完成后运行以下命令检查是否添加了purge 模块
docker run -it --rm resty1.27.proxy_cache nginx -V
我已经构建完镜像并运行了一个容器开始写项目了,最后的 nginx -V:
[root@6939794efece /]# nginx -V
nginx version: openresty/1.27.1.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 3.0.15 3 Sep 2024
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.3 --add-module=../echo-nginx-module-0.63 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.33 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.09 --add-module=../srcache-nginx-module-0.33 --add-module=../ngx_lua-0.10.27 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.37 --add-module=../array-var-nginx-module-0.06 --add-module=../memc-nginx-module-0.20 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.9 --add-module=../rds-json-nginx-module-0.17 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.15 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-openssl=/tmp/openssl-3.0.15 --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --with-http_v3_module --with-ipv6 --with-md5-asm --with-sha1-asm --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_slice_module --with-threads --with-compat --with-http_ssl_module --add-module=/tmp/ngx_cache_purge --with-openssl-opt=-g --with-stream
后续代理缓存的实战代码,有时间再更新。
本作品采用《CC 协议》,转载必须注明作者和本文链接
关于 LearnKu
推荐文章: