openresty 编译ngx_pagespeed 模块-docker 构建

ngx_pagespeed 是一个很不错的web 优化模块,我们通过简单的配置就可以对于web页面的加载有很大的提升
ngx_pagespeed 依赖psol 模块

Dockerfile

 
# Dockerfile - Debian Jessie
# https://github.com/openresty/docker-openresty
# ARG RESTY_IMAGE_BASE="ubuntu"
# ARG RESTY_IMAGE_TAG="xenial"
ARG RESTY_IMAGE_BASE="debian"
ARG RESTY_IMAGE_TAG="stretch-slim"
FROM ${RESTY_IMAGE_BASE}:${RESTY_IMAGE_TAG} as nginx
LABEL maintainer="rongfengliang"
# Docker Build Arguments
ARG RESTY_VERSION="1.13.6.1"
ARG RESTY_LUAROCKS_VERSION="2.4.3"
ARG RESTY_OPENSSL_VERSION="1.0.2k"
ARG RESTY_PCRE_VERSION="8.41"
ARG RESTY_J="1"
ARG RESTY_CONFIG_OPTIONS="
    --with-file-aio 
    --with-http_addition_module 
    --with-http_auth_request_module 
    --with-http_dav_module 
    --with-http_flv_module 
    --with-http_geoip_module=dynamic 
    --with-http_gunzip_module 
    --with-http_gzip_static_module 
    --with-http_image_filter_module=dynamic 
    --with-http_mp4_module 
    --with-http_random_index_module 
    --with-http_realip_module 
    --with-http_secure_link_module 
    --with-http_slice_module 
    --with-http_ssl_module 
    --with-http_stub_status_module 
    --with-http_sub_module 
    --with-http_v2_module 
    --with-http_xslt_module=dynamic 
    --with-ipv6 
    --with-mail 
    --with-mail_ssl_module 
    --with-md5-asm 
    --with-pcre-jit 
    --with-sha1-asm 
    --with-stream 
    --with-stream_ssl_module 
    --with-threads 
    --add-module=/tmp/incubator-pagespeed-ngx-1.13.35.2-stable 
    "
ARG RESTY_CONFIG_OPTIONS_MORE=""
# These are not intended to be user-specified
ARG _RESTY_CONFIG_DEPS="--with-openssl=/tmp/openssl-${RESTY_OPENSSL_VERSION} --with-pcre=/tmp/pcre-${RESTY_PCRE_VERSION}"
# 1) Install apt dependencies
# 2) Download and untar OpenSSL, PCRE, and OpenResty
# 3) Build OpenResty
# 4) Cleanup
RUN DEBIAN_FRONTEND=noninteractive apt-get update 
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends 
        build-essential 
        ca-certificates 
        curl 
        gettext-base 
        libgd-dev 
        libgeoip-dev 
        libncurses5-dev 
        libperl-dev 
        libreadline-dev 
        libxslt1-dev 
        make 
        perl 
        unzip 
        uuid-dev 
        zlib1g-dev 
    && cd /tmp 
    && curl -fSL https://www.openssl.org/source/openssl-${RESTY_OPENSSL_VERSION}.tar.gz -o openssl-${RESTY_OPENSSL_VERSION}.tar.gz 
    && tar xzf openssl-${RESTY_OPENSSL_VERSION}.tar.gz 
    && curl -fSL https://ftp.pcre.org/pub/pcre/pcre-${RESTY_PCRE_VERSION}.tar.gz -o pcre-${RESTY_PCRE_VERSION}.tar.gz 
    && tar xzf pcre-${RESTY_PCRE_VERSION}.tar.gz 
    && curl -fSL https://github.com/apache/incubator-pagespeed-ngx/archive/v1.13.35.2-stable.tar.gz -o nginx-pagespeed-1.13.35.2.tar.gz 
    && tar xzf nginx-pagespeed-1.13.35.2.tar.gz 
    && curl -fSL https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz -o 1.13.35.2-x64.tar.gz 
    && tar -xzvf 1.13.35.2-x64.tar.gz 
    && cp -rf psol /tmp/incubator-pagespeed-ngx-1.13.35.2-stable 
    && curl -fSL https://openresty.org/download/openresty-${RESTY_VERSION}.tar.gz -o openresty-${RESTY_VERSION}.tar.gz 
    && tar xzf openresty-${RESTY_VERSION}.tar.gz 
    && cd /tmp/openresty-${RESTY_VERSION} 
    && ./configure -j${RESTY_J} ${_RESTY_CONFIG_DEPS} ${RESTY_CONFIG_OPTIONS} ${RESTY_CONFIG_OPTIONS_MORE} 
    && make -j${RESTY_J} 
    && make -j${RESTY_J} install 
    && cd /tmp 
    && rm -rf 
        openssl-${RESTY_OPENSSL_VERSION} 
        openssl-${RESTY_OPENSSL_VERSION}.tar.gz 
        openresty-${RESTY_VERSION}.tar.gz openresty-${RESTY_VERSION} 
        pcre-${RESTY_PCRE_VERSION}.tar.gz pcre-${RESTY_PCRE_VERSION} 
    && curl -fSL https://github.com/luarocks/luarocks/archive/${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 
        --lua-suffix=jit-2.1.0-beta3 
        --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 
    && DEBIAN_FRONTEND=noninteractive apt-get autoremove -y 
    && ln -sf /dev/stdout /usr/local/openresty/nginx/logs/access.log 
    && ln -sf /dev/stderr /usr/local/openresty/nginx/logs/error.log
FROM ${RESTY_IMAGE_BASE}:${RESTY_IMAGE_TAG}
LABEL maintainer="11415191465@qq.com" 
      version.mod-pagespeed="1.13.35.2" 
      version.ngx-pagespeed="1.13.35.2"
COPY --from=nginx /usr/local/openresty /usr/local/openresty
COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf
ENV PATH=$PATH:/usr/local/openresty/luajit/bin:/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin
RUN ln -sf /dev/stdout /usr/local/openresty/nginx/logs/access.log
RUN ln -sf /dev/stderr /usr/local/openresty/nginx/logs/error.log
STOPSIGNAL SIGQUIT
ENV PATH=$PATH:/usr/local/openresty/luajit/bin:/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin
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.0-beta3/?.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"
# Copy nginx configuration files
COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf
# TODO: remove any other apt packages?
CMD ["/usr/local/openresty/bin/openresty", "-g", "daemon off;"]
# Use SIGQUIT instead of default SIGTERM to cleanly drain requests
# See https://github.com/openresty/docker-openresty/blob/master/README.md#tips--pitfalls
 

说明

构建使用了docker 的multi stage 方式,对于依赖的psol 手工下载,并copy ngx_pagespeed 模块
目录下,构建完成之后,直接copy 构建之后的文件到一个新的基础镜像中,这样可以减少容器镜像
的大小,这样我们既可以使用openresty 方便的工具,同时也可以使用pagespeed 进行web的快速优化
了,dockerfile 参考了openresty github 仓库,一个具体的使用例子可以参考:
https://github.com/rongfengliang/nginx-ssi-pagespeed/tree/master/ssi-pagespeed-customimage

参考资料

https://github.com/rongfengliang/nginx-ssi-pagespeed
https://github.com/apache/incubator-pagespeed-ngx
https://github.com/openresty/docker-openresty
https://www.modpagespeed.com/

https://github.com/rongfengliang/openresty-pagespeed-docker