1、简介
WebDAV (Web-based Distributed Authoring and Versioning) 一种基于 HTTP 1.1协议的通信协议。它扩展了HTTP 1.1,在GET、POST、HEAD等几个HTTP标准方法以外添加了一些新的方法,使应用程序可直接对Web Server直接读写,并支持写文件锁定(Locking)及解锁(Unlock),还可以支持文件的版本控制,本章使用nginx加载模块webdav实现此功能。
实验步骤
1、下载ngx-dav-ext-module
wget https://codeload.github.com/arut/nginx-dav-ext-module/zip/master
或者
git clone --recursive https://github.com/arut/nginx-dav-ext-module
cd /usr/local/src/ && unzip nginx-dav-ext-module-master.zip #解压ngx-dav-ext-module
2、重新编译Nginx
这里我已经将Nginx提前安装完毕了(使用的是Yum安装的), 但是我们需要重新编译安装,将ngx-dav-ext-module加入到Nginx中。
查看原有的编译参数
/usr/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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 --user=nginx --group=nginx --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 --with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC’ --with-ld-opt=’-Wl,-z,relro -Wl,-z,now -pie’
将原有的Nginx备份
mv /usr/sbin/nginx /usr/sbin/nginx.bak
cp /etc/nginx{,.bak} -rf
下载和自己原有的Nginx版本相同的源码包(如:1.18.0), 并解压
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar zxf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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 --user=nginx --group=nginx --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 --with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC’ --with-ld-opt=’-Wl,-z,relro -Wl,-z,now -pie’ --with-http_dav_module --add-module=/usr/local/src/nginx-dav-ext-module-master # 重新编译并且添加两个选项–with-http_dav_module --add-module=/usr/local/src/nginx-dav-ext-module-master
如果出现了如下错误:
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel
然后在重新编译
make -j8
可以使用 下面的命令查看上一条命令 是否成功
echo ?$
在这需要记住千万不要make install
不然会对原有的nginx覆盖出现问题
替换原有的nginx
cp -rf /usr/local/src/nginx-1.18.0/objs/nginx /usr/sbin/
通过如下图片可以看到我们添加的模块已经成功了
3、创建配置文件
vim /etc/nginx/conf.d/webdav.conf
server {
listen 80;
server_name webdav.chinaedu.net;
access_log /var/log/nginx/webdav.access.log main;
location / {
root /data/sqldown/;
autoindex on;
dav_methods PUT DELETE MKCOL COPY MOVE;
dav_ext_methods PROPFIND OPTIONS;
create_full_put_path on;
dav_access user:rw group:r all:r;
auth_basic "Authorized Users Only";
auth_basic_user_file /etc/nginx/.htpasswd;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
mkdir -p /data/sqldown/
chown nginx:nginx /data/sqldown/
chmod 700 /data/sqldown/
4、配置身份验证
4.2、安装httpd工具
yum -y install httpd-tools
4.3、创建密码文件和密码
htpasswd -c /etc/nginx/.htpasswd user1
[root@yearning sqldown]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@yearning sqldown]# nginx -s reload
5、使用curl测试webdav:
上传文件:
curl -T filename https://webdav.chinaedu.net/
重命名文件:
curl -X MOVE --header 'Destination:https://webdav.chinaedu.net/newname' https://webdav.chinaedu.net/filename
删除文件:
curl -X DELETE https://webdav.chinaedu.net/filename