openresty / docker-openresty

Docker tooling for OpenResty
https://hub.docker.com/r/openresty/openresty
BSD 2-Clause "Simplified" License
935 stars 525 forks source link

The dynamic module of nginx cannot be configured #217

Open mpv945 opened 1 year ago

mpv945 commented 1 year ago

Brotli is dynamically compiled into a so file, and load_module /etc/nginx/ngx_http_brotli_filter_module.so; and load_module /etc/nginx/ngx_http_brotli_static_module.so are added at the beginning of nginx.conf

neomantra commented 1 year ago

I see what you are saying. Besides the http section, the main stanza is currently:

pcre_jit on;
events {
    worker_connections  1024;
}

You would like to inject some other text there, like a load_module line. We do let you alter the build process with Docker build arguments, like RESTY_EVAL_POST_MAKE. So you could use that arg to do something like:

docker build --build-arg RESTY_EVAL_POST_MAKE="echo 'load_module /etc/nginx/ngx_http_brotli_filter_module.so; load_module /etc/nginx/ngx_http_brotli_static_module.so;' >> /usr/local/openresty/nginx/conf/nginx.conf" -f focal/Dockerfile .

I didn't actually test that, but it's the idea.


You can also bind-mount your own nginx.conf to /usr/local/openresty/nginx/conf/nginx.conf. You are also bind-mounting the shared-objects or building it in a derived image?

mpv945 commented 1 year ago

Can docker-openresty support brotli compression?

neomantra commented 1 year ago

I see the module ngx_brotli. Is there any reason this isn't already in Nginx or OpenResty already? I generally don't add modules, but maybe we can add it to the -fat images?

dixyes commented 1 year ago

Offical docker image of PHP provides ENV like "PHPIZE_DEPS" and configured php sources, this make user build modules after base built possible:

  1. base image provides ENV like configure arguments, build deps
  2. base image provides configured sources and tar it, save it to /usr/src
  3. when use FROM that base image, user can use distro_pm_like_apk add $BUILD_DEPS install build deps, then tar -xf /usr/src/configured.tar.zst -C /usr/src/openresty and cd into it, make their own modules ./configure $CONFIG_THINGS --add-dynamic-module=/path/to/whatever
  4. after that, user can copy modules from that stage or distro_pm_like_apk del $BUILD_DEPS
  5. at last, user install running deps like libbrotlidec for ngx-brotli
dixyes commented 1 year ago

Here's my demo Dockerfile for that

Since we donot have DEPS and CONFIG env, so this is rather fragile:

FROM openresty/openresty:1.21.4.1-4-rocky AS module-builder

# DONOT USE THIS IN PRODUCT!

WORKDIR /usr/src

RUN set -xeo pipefail ; \
    dnf install -y git && \
    wget https://openresty.org/download/openresty-1.21.4.1.tar.gz && \
    mkdir openresty && \
    tar --strip-components=1 -xf openresty-1.21.4.1.tar.gz -C openresty && \
    git clone https://github.com/google/ngx_brotli /usr/src/ngx_brotli && \
    git clone https://github.com/tokers/zstd-nginx-module /usr/src/zstd-nginx-module && \
    :

RUN dnf install -yy epel-release && \
    dnf install -yy ccache gcc make brotli-devel libzstd-devel \
        # here we donot know which packages is used in building openresty
        # so we have to dnf search and append "-devel" here
        openresty-openssl111-devel openresty-pcre-devel openresty-zlib-devel

RUN set -xeo pipefail ; \
    cd /usr/src/openresty/bundle/nginx-1.21.4 && \
    # here we cannot get real configure args so we have to use nginx -V
    # futhermore, --add-modules things make configure fail because wo donot have a "configurable" source for them
    RESTY_CONFIG_OPTIONS="$(nginx -V 2>&1 | grep 'configure arguments:' | sed 's/^configure arguments://g' | sed -E 's/--add-module=[^ ]+ //g')" && \
    eval "./configure $RESTY_CONFIG_OPTIONS --add-dynamic-module=/usr/src/ngx_brotli --add-dynamic-module=/usr/src/zstd-nginx-module" && \
    make -j `nproc` modules && \
    :

FROM openresty/openresty:1.21.4.1-4-rocky

RUN dnf install -yy brotli libzstd && \
    dnf clean all

COPY --from=module-builder \
    /usr/src/openresty/bundle/nginx-1.21.4/objs/ngx_http_brotli_filter_module.so \
    /usr/src/openresty/bundle/nginx-1.21.4/objs/ngx_http_brotli_static_module.so \
    /usr/src/openresty/bundle/nginx-1.21.4/objs/ngx_http_zstd_filter_module.so \
    /usr/src/openresty/bundle/nginx-1.21.4/objs/ngx_http_zstd_static_module.so \
    /usr/local/openresty/nginx/modules/