nginx搭建webdav

722 阅读1分钟

环境

  • win11 挂载机
  • centos7 webdav服务机器
  • nginx 1.21.2
  • raidrive 挂载工具

安装nginx和webdav模块

image.png

这里翻译的意思是,nginx只支持webdav简单的方法PUT,DELETE...等方法,但是想要使用更多的功能需要使用--with-http_dav_module

# 这里使用的版本,实际情况按照官网去更新
wget https://nginx.org/download/nginx-1.22.2.tar.gz
tar -zxvf nginx-1.22.2.tar.gz
cd nginx-1.22.2

# 获取webdab模块
git clone --recursive https://github.com/arut/nginx-dav-ext-module

#获取去头模块
wget https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz
#解压
tar -zxvf v0.33.tar.gz

./configure --with-http_dav_module --add-module=./nginx-dav-ext-module --add-module=./headers-more-nginx-module-0.33

make

make install

/usr/local/nginx/sbin/nginx -V

查看结果:

image.png

到这里安装就算成功了

安装过程中可能会有缺少依赖的情况

# /configure: error: the HTTP XSLT module requires the libxml2/libxslt
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel

# 其他少的需要一个一个找

配置nginx和webdav相关的目录

创建目录

# 创建目录
mkdir -p /home/www/webdav
chown -R nobody:nobody /home/www/webdav
chmod -R 700 /home/www/webdav





创建密码

echo 用户名:$(openssl passwd -crypt 密码)>/usr/local/nginx/webdavpasswd

配置nginx

# 配置nginx
location / {
			client_max_body_size 5G; # 最大允许上传文件大小
			root /home/www/webdav;
			# index index.html index.htm;
			autoindex on;
			# autoindex_localtime on;
			set $dest $http_destination;
			# 对目录请求、对URI自动添加"/" 
			if (-d $request_filename) {  
				rewrite ^(.*[^/])$ $1/; 
				set $dest $dest/; 
			}
			# 对MOVE|COPY方法强制添加Destination请求头 
			if ($request_method ~ (MOVE|COPY)) {
				more_set_input_headers 'Destination: $dest'; 
			}
			if ($request_method ~ MKCOL) { 
				rewrite ^(.*[^/])$ $1/ break; 
			}
			# webdav config
			client_body_temp_path /tmp;
			dav_methods PUT DELETE MKCOL COPY MOVE; #DAV支持的请求方法
			dav_ext_methods PROPFIND OPTIONS LOCK UNLOCK; # DAV扩展支持的请求方法
			create_full_put_path on;  # 启用创建目录支持
			dav_access group:rw all:r; # 创建文件的以及目录的访问权限
			auth_basic "Authorized Users Only";
			# auth_basic "Authorized Users WebDAV";
			# auth_basic "user login";
			auth_basic_user_file /usr/local/nginx/webdavpasswd;
}

启动nginx

/usr/local/nginx/sbin/nginx

使用railDrive

windows自带的不好用,上传文件容易报错,所以直接用第三方软件

image.png

配置好了之后效果

image.png