介绍
- 使用nginx的webdav,添加相关第三方模块组件
- 以下场景是部署在docker下的,当然,dockerfile里的内容,完全可以转换成脚本去执行
- 综上,你完全可以参考本教程在其他环境进行搭建
- 这里没有列举失败案例,需要自行学习
- 部分图片来源网络
参考
Nginx WebDAV模块配置简述
Github nginx-dav-ext-module
Github headers-more-nginx-module
Nginx里Header修改
配置基于Nginx的WebDav服务
Nginx 反向代理 WebDAV 服务 不能上传、新建、重命名文件及文件夹的解决办法
大概过程
- 新建目录,并且准备Dockerfile文件,文件名就叫Dockerfile
- 在该目录下通过docker build命令创建镜像,耗时比较久,网络不行可能会失败
- 准备nginx.conf配置文件和用户密码文件
- 拉起镜像,镜像正常工作则表示完成
- 登录使用!
抄作业
Dockfile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| FROM alpine AS build RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \ && apk --update add git gcc g++ make wget pcre2-dev libxslt-dev \ && NGINX_VERSION=nginx-1.29.0 \ && cd /usr/local/ \ && mkdir build nginx \ && cd build \ && wget http://nginx.org/download/${NGINX_VERSION}.tar.gz \ && tar -xf ${NGINX_VERSION}.tar.gz \ && git clone --depth=1 https://github.com/openresty/headers-more-nginx-module.git \ && git clone --depth=1 https://github.com/arut/nginx-dav-ext-module.git \ && cd ${NGINX_VERSION} \ && ./configure --prefix=/usr/local/nginx --with-http_dav_module --add-module=../nginx-dav-ext-module --add-module=../headers-more-nginx-module \ && make -j 4 \ && make install \ && START_CMD="/usr/local/nginx/sbin/startup" \ && echo '#!/bin/sh' > ${START_CMD} \ && echo '! id $CUSTOM_USER > /dev/null 2>&1 && adduser -D -u 2000 -s /sbin/nologin $CUSTOM_USER' >> ${START_CMD} \ && echo "nginx -g 'daemon off;'" >> ${START_CMD} \ && chmod 755 ${START_CMD}
FROM alpine COPY --from=build ["/usr/local/nginx", "/usr/local/nginx"] RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \ && apk add --update --no-cache tzdata pcre2-dev libxslt-dev \ && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH=/usr/local/nginx/sbin:$PATH EXPOSE 80/tcp
ENTRYPOINT ["startup"]
|
制作镜像:
- 需要在Dockerfile的目录下执行该命令
- 注意命令后面的有个点,代表当前目录
sudo docker build -t nginx-dav .
不出意外的话会成功,出意外的话自己百度,基本技能!
nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| user davuser; worker_processes 1;
pid /run/nginx.pid;
events { worker_connections 768; # multi_accept on; }
http { include mime.types; default_type application/octet-stream;
sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048;
access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log;
gzip on;
map $http_destination $webdav_dest { ~*https://(.+) http://$1; default $http_destination; }
dav_ext_lock_zone zone=davlock:10m; # DAV文件锁内存共享区 server { listen 80; server_name localhost;
charset utf-8;
root /mnt;
auth_basic realm_name;
#用户密码存放位置 auth_basic_user_file /usr/local/share/nginx/conf/webpasswd;
location /{ #dav允许的操作 dav_methods PUT DELETE MKCOL COPY MOVE; dav_ext_methods PROPFIND OPTIONS LOCK UNLOCK; dav_ext_lock zone=davlock; # DAV扩展锁绑定的内存区域
set $dest $http_destination; if (-d $request_filename) { # 对目录请求、对URI自动添加"/" rewrite ^(.*[^/])$ $1/; set $dest $dest/; }
# 解决webdav不能创建文件夹问题 if ($request_method = MKCOL) { rewrite ^(.*[^/])$ $1/ break; }
# 解决webdav不能重命名问题 if (-d $request_filename) { rewrite ^(.*[^/])$ $1/; set $webdav_dest $webdav_dest/; more_set_input_headers 'Destination: $webdav_dest'; }
# 解决webdav不能复制、移动文件问题 if ($request_method ~ (MOVE|COPY)) { more_set_input_headers 'Destination: $webdav_dest'; }
#创建文件默认的权限 dav_access user:rw;
#临时文件的默认权限 client_body_temp_path /tmp;
#最大上传文件限制,0表示无限制 client_max_body_size 0;
#允许自动创建文件夹 create_full_put_path on; #autoindex on; #autoindex_localtime on; } } }
|
创建密码文件
这里创建密码中的用户名需要和环境变量CUSTOM_USER定义的用户名一致
1 2 3 4 5
| # 创建新用户user echo -n 'davuser:' | tee ./webpasswd # 设定用户密码 openssl passwd -apr1 | tee -a ./webpasswd Password: # 输入用户的密码
|
拉起镜像
每一项-v参数冒号前面的路径是你的实际路径,后面的路径不变
环境变量CUSTOM_USER定义的用户名需要和创建密码中的用户名一致
1 2 3 4 5 6 7 8 9 10
| docker run -d \ --name nginx-webdav \ --restart unless-stopped \ -e CUSTOM_USER=dream \ -p 80:80/tcp \ -v /path/to/nginx.conf:/usr/local/share/nginx/conf/nginx.conf:ro \ -v /path/to/webpasswd:/usr/local/share/nginx/conf/webpasswd:ro \ -v /path/to/sharedir:/mnt \ -v /var/log/nginx-dav:/var/log/nginx \ nginx-dav
|
结束
镜像拉起后就可以使用了,使用方法这里不赘述!感谢观看!
更新日志
- 修改Dockerfile内容,从基于ubuntu修改为基于alpine镜像,以减少镜像体积
- 修改nginx的配置文件,将创建的线程数量设置为1,默认auto表示按cpu个数拉起线程
- 修改启动命令,新增用户名设置,限制配置文件只读映射,增加日志导出绑定,由于镜像创建了时区,所以命令中去掉时区文件映射