构建一个包含fancyindex的nginx docker镜像

构建fancyindex镜像

1. 下载nginx和fancyindex的源码并解压

wget http://nginx.org/download/nginx-1.29.7.tar.gz
wget https://github.com/aperezdc/ngx-fancyindex/releases/download/v0.6.0/ngx-fancyindex-0.6.0.tar.xz
tar -xzvf nginx-1.29.7.tar.gz && tar -xvJf ngx-fancyindex-0.6.0.tar.xz

2. 新建一个Dockerfilenano Dockerfile内容如下

FROM alpine:latest
# 维护者信息
LABEL maintainer="kob@qq.com"
WORKDIR /root
 
# 复制 Nginx 源码
COPY ./nginx-1.29.7 /root/nginx-1.29.7
COPY ./ngx-fancyindex-0.6.0 /root/ngx-fancyindex-0.6.0
 
# 创建系统用户和组
RUN addgroup -S -g 101 nginx \
    && adduser -S -D -H -s /bin/false -u 101 -G nginx -h /nonexistent -g "nginx user" nginx \
 
    && apk update \
    && apk add --no-cache \
        gcc \
        musl-dev \
        pcre-dev \
        openssl-dev \
        zlib-dev \
        make \
        curl \
        linux-headers \
        perl \
        libaio-dev \
 
    && cd /root/nginx-1.29.7/ && \
    ./configure \
        --prefix=/etc/nginx \
        --sbin-path=/usr/sbin/nginx \
        --modules-path=/usr/lib/nginx/modules \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
        --pid-path=/var/run/nginx.pid \
        --lock-path=/var/run/nginx.lock \
        --http-client-body-temp-path=/var/cache/nginx/client_temp \
        --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
        --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
        --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
        --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
        --with-perl_modules_path=/usr/lib/perl5/vendor_perl \
        --user=nginx \
        --group=nginx \
        --with-compat \
        --with-threads \
        --with-http_addition_module \
        --with-http_auth_request_module \
        --with-http_dav_module \
        --with-http_flv_module \
        --with-http_gunzip_module \
        --with-http_gzip_static_module \
        --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_v3_module \
        --with-mail \
        --with-mail_ssl_module \
        --with-stream \
        --with-stream_realip_module \
        --with-stream_ssl_module \
        --with-stream_ssl_preread_module \
        --with-cc-opt='-Os -fstack-clash-protection -Wformat -Werror=format-security -fno-plt -g' \
        --with-ld-opt='-Wl,--as-needed,-O1,--sort-common -Wl,-z,pack-relative-relocs' \
        --add-module=../ngx-fancyindex-0.6.0 \
    && make -j$(nproc) \
    && make install \
    && mkdir -p /var/cache/nginx/client_temp \
    && chown -R nginx:nginx /var/cache/nginx \
    && rm -rf /root/nginx-1.29.7 \
    && rm -rf /root/ngx-fancyindex-0.6.0 \
# 设置时区为东八区
    && apk add --no-cache tzdata \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
#    && apk del tzdata gcc musl-dev pcre-dev openssl-dev zlib-dev make curl linux-headers perl libaio-dev \
    && apk del tzdata gcc make curl perl \
# 清理缓存
    && rm -rf /var/cache/apk/*
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]

3. 构建镜像

docker build -t nginx-fancyindex:alpine .

4. 上传到Docker hub

docker login
docker tag nginx-fancyindex:alpine perlongqq/nginx-fancyindex:alpine
docker push perlongqq/nginx-fancyindex:alpine

使用

主题下载地址

https://github.com/Naereen/Nginx-Fancyindex-Theme

配置文件

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl;
    listen [::]:443 ssl;
    listen 443 quic;
    listen [::]:443 quic;
    # 开放的访问地址
    server_name 127.0.0.1 your.domain.com;

    # SSL 证书配置
    ssl_certificate /etc/nginx/certs/***_cert.pem;
    ssl_certificate_key /etc/nginx/certs/***_key.pem;

    # 指定字符编码
    charset utf-8;

    # HTTP 重定向到 HTTPS
#    if ($scheme = http) {
#        return 301 https://$server_name$request_uri;
#    }

    # 根目录和主页设置
    root /var/www/html/files;
    index index.html;

    # 主页面和静态资源的缓存
    location / {
        aio threads;
        add_header Cache-Control "public, max-age=2592000";
        add_header Alt-Svc 'h3=":443"; ma=86400';
        fancyindex on;
        fancyindex_localtime on;
        fancyindex_exact_size off;
# Specify the path to the header.html and foother.html files, that are server-wise,
# ie served from root of the website. Remove the leading '/' otherwise.
# 指定头文件和脚文件的位置
        fancyindex_header "/Nginx-Fancyindex-Theme-dark/header.html";
        fancyindex_footer "/Nginx-Fancyindex-Theme-dark/footer.html";
# Ignored files will not show up in the directory listing, but will still be public.
# 忽略文件
        fancyindex_ignore "examplefile.html";
# Making sure folder where these files are do not show up in the listing.
        fancyindex_ignore "Nginx-Fancyindex-Theme-light";
        fancyindex_ignore "Nginx-Fancyindex-Theme";
    }

    # 客户端最大请求体限制
    client_max_body_size 50m;
}
perlong
“ 一天一个新发现 ”
 喜欢文章
1人喜欢
头像