1、下载 Fancyindex[1]
登录到服务器,进入工作目录:
/data/compose/nginx
该目录可自定义,尽量不要在系统盘,防止系统盘损坏导致数据丢失。本文后续下载和新建的文件都基于此目录。
$ wget -O fancyindex.zip https://github.com/aperezdc/ngx-fancyindex/archive/refs/tags/v0.5.2.zip
$ unzip fancyindex.zip
ngx-fancyindex-0.5.2
2、下载 Nginx-Fancyindex-Theme[2]
开源的 fancyindex 主题,支持搜索,界面美化,有白色/黑色两种风格。
$ wget -O fancytheme.zip https://github.com/Naereen/Nginx-Fancyindex-Theme/archive/master.zip
$ unzip fancytheme.zip
Nginx-Fancyindex-Theme-master
$ mv Nginx-Fancyindex-Theme-master Nginx-Fancyindex-Theme
3、下载 Nginx[3]
可以自定义版本,1.21.4 是当前最新版本,亲测可用。
$ wget http://nginx.org/download/nginx-1.21.4.tar.gz
$ tar xvf nginx-1.21.4.tar.gz
# 将 fancyindex 放到 nginx 目录内,方便后续编译安装
$ mkdir -p nginx-1.21.4/model
$ mv ngx-fancyindex-0.5.2/ nginx-1.21.4/model/ngx-fancyindex-0.5.2
# nginx 默认配置
$ cp -r nginx-1.21.4/html nginx && cp nginx-1.21.4/conf/* nginx
4、编写 nginx.conf 配置文件
$ vim ./nginx-1.21.4/conf/nginx.conf
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
fancyindex on; # 使用fancyindex
fancyindex_exact_size off; # 不显示精确大小
fancyindex_time_format "%Y-%m-%d %H:%M:%S";# 文件日期
fancyindex_localtime on;# 使用用户本地时间
fancyindex_default_sort date_desc; # 默认时间排序
# fancyindex_header "/Nginx-Fancyindex-Theme-light/header.html"; # 头
# fancyindex_footer "/Nginx-Fancyindex-Theme-light/footer.html"; #
# fancyindex_ignore "examplefile.html"; # 忽略文件
# fancyindex_ignore "Nginx-Fancyindex-Theme-light";
# fancyindex_name_length 255;
include /etc/nginx/conf.d/*.conf;
}
5、Dockerfile
添加 FancyIndex 模块的功能需要重新编译 Nginx,因此没有使用官方现成的 Nginx 镜像,编写 Dockerfile,构建新镜像。
FROM alpine:latest
LABEL author="lan"
WORKDIR /root
COPY ./nginx-1.21.4 /usr/local/nginx-1.21.4
# 配置中科大下载源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
# 更新 & 安装依赖
&& apk update \
&& apk add --no-cache gcc libc-dev make openssl-dev pcre-dev zlib-dev linux-headers curl \
&& cd /usr/local/nginx-1.21.4/ \
# 执行编译安装 Nginx
&& ./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 \
--conf-path=/etc/nginx/nginx.conf \
--with-http_ssl_module \
# 指定安装扩展模块路径
--add-module=/usr/local/nginx-1.21.4/model/ngx-fancyindex-0.5.2 \
--with-compat \
--with-file-aio \
--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-mail \
--with-mail_ssl_module \
--with-stream --with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
&& make \
&& make install \
&& mkdir -p /var/cache/nginx/client_temp \
&& rm -rf /usr/local/nginx-1.21.4 \
# 修改系统时区为东八区
&& rm -rf /etc/localtime \
&& ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 80
CMD ["/bin/sh","-c","nginx -g 'daemon off;'"]
构建镜像
# 命令记不住可以直接放到 sh 文件
$ docker build -t nginx-fancyindex:0.0.1 -f Dockerfile .
...
Successfully built 93d160ece5d8
Successfully tagged nginx-fancyindex:0.0.1
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-fancyindex 0.0.1 9fb281056a37 28 minutes ago 190MB
6、编写 docker-compose.yml 配置文件
version: "3.3"
services:
nginx-fancyindex:
image: nginx-fancyindex:0.0.1
container_name: nginx-fancyindex
restart: always
ports:
- 80:80
volumes:
- /etc/localtime:/etc/localtime:ro
# 映射nginx目录
- /data/compose/nginx/nginx:/etc/nginx
# 映射日志目录
- /data/compose/nginx/logs:/var/log/nginx
# 映射fancyindex主题静态资源
# - /data/compose/nginx/Nginx-Fancyindex-Theme:/etc/nginx/html
7、启动容器
$ docker-compose up -d
Creating network "nginx_default" with the default driver
Creating nginx-fancyindex ...
Creating nginx-fancyindex ... done
# 删除并重新创建容器
$ docker-compose down && docker-compose up -d && docker-compose logs -f
最终效果
GitHub - fraoustin/fancyindex: container docker for run nginx server with module fancyindex
Docker - 搭建 nginx 服务 - 知乎