環(huán)境
宿主機(jī): window10
服務(wù)器: CentOS7(Linux)
Nginx : 1.18.0
網(wǎng)盤映射: RaiDrive
前言
- 什么是Webdav
引用維基百科的解釋:
基于Web的分布式編寫和版本控制(WebDAV)是超文本傳輸協(xié)議(HTTP)的擴(kuò)展管怠,有利于用戶間協(xié)同編輯和管理存儲在萬維網(wǎng)服務(wù)器文檔几晤。WebDAV由互聯(lián)網(wǎng)工程任務(wù)組的工作組在RFC 4918中定義钙姊。
WebDAV擴(kuò)展了request方法所允許的標(biāo)準(zhǔn)HTTP謂詞和HTTP頭柬焕。增加的謂詞包括:
(1).COPY: 將資源從一個URI復(fù)制到另一個URI
(2).LOCK:鎖定一個資源哥蔚,webdav支持共享鎖和互斥鎖
(3).MKCOL: 創(chuàng)建集合(即目錄)
(3).MOVE: 將資源從一個URI移動到另一個URI
(4).PROPFIND: 從Web資源中檢索以XML格式存儲的屬性册烈。它也被重載倘屹,以允許一個檢索遠(yuǎn)程系統(tǒng)的集合結(jié)構(gòu)(也叫目錄層次結(jié)構(gòu))候址。
(5).PROPPATCH: 在單個原子性動作中更改和刪除資源的多個屬性
(6).UNLOCK: 解除資源的鎖定
- 2.Nginx-dav-ext-module是用來干嘛的
官方介紹:
The standard ngx_http_dav_module provides partial WebDAV implementation and only supports GET,HEAD,PUT,DELETE,MKCOL,COPY,MOVE methods.
For full WebDAV support in nginx you need to enable the standard ngx_http_dav_module as well as this module for the missing methods.
大致意思就是,標(biāo)準(zhǔn)的ngx_http_dav_module 提供了部分的WebDAV是實現(xiàn)族扰,只支持GET厌丑,HEAD, PUT定欧,DELETE,MKCOL,COPY,MOVE 方法,但是為了在Nginx中獲取完整的WebDAV支持怒竿,就需要啟用標(biāo)準(zhǔn)的ngx_http_dav_module這個模塊來處理缺少的方法砍鸠。
構(gòu)建
下載ngx-dav-ext-module
如果你的服務(wù)器上安裝了Git可以直接使用下面的命令:
cd /usr/local/src && git clone --recursive https://github.com/arut/nginx-dav-ext-module
如果沒有Git 也可以將其在宿主機(jī)上下載完成后,上傳至服務(wù)器耕驰。
重新編譯Nginx
這里我已經(jīng)將Nginx提前安裝完畢了(使用的是Yum安裝的), 但是我們需要重新編譯安裝爷辱,將ngx-dav-ext-module加入到Nginx中。
- 1.查看原有的編譯參數(shù)
[root@xxxx ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (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' \
--add-module=/usr/local/src/ngx_cache_purge-2.3 \
--add-module=/usr/local/src/headers-more-nginx-module-0.33
- 2.將原有的Nginx備份
mv /usr/sbin/nginx /usr/sbin/nginx.bak
- 3.下載和自己原有的Nginx版本相同的源碼包(如:1.18.0)朦肘, 并解壓
tar -xzvg nginx-1.18.0.tar.gz
cd nginx-1.18.0
- 4.對新版重新編譯配置
根據(jù)第一步拿到自己的原有的配置饭弓,并在后面加入如下的配置:
....
+ --with-http_dav_module --add-module=/usr/local/src/nginx-dav-ext-module
這里的路徑一定是要你放置ngx-dav-ext-module的位置,不然后面編譯會出現(xiàn)無法找到該路徑
※ 如果出現(xiàn)了如下錯誤:
/configure: error: the HTTP XSLT module requires the libxml2/libxslt
說明缺少依賴:
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel
- 5.編譯
make -j12
# 可以使用 下面的命令查看上一條命令 是否成功
echo ?$
- 5.進(jìn)入obj目錄媒抠,替換原有的Nginx
cp objs/nginx /usr/local/nginx/sbin/nginx
確認(rèn)模塊啟用
nginx -V
可見配置的參數(shù)后面有加載模塊:
--with-http_dav_module --add-module=/usr/local/src/nginx-dav-ext-module
創(chuàng)建上傳目錄
mkdir -p /home/www/webdav
# 設(shè)置文件所有者 如果是所有人都可以使用的話設(shè)置 nobody:nobody
chown nginx:nginx /home/www/webdav
# 設(shè)置文件權(quán)限
chmod -R 700 /home/www/webdav
配置身份驗證
- 1.安裝httpd工具
yum -y install httpd-tools
- 2.創(chuàng)建密碼文件
htpasswd -c /opt/soft/.htpasswd dav
# 如果還有用戶
htpasswd /opt/soft/.htpasswd user1
- 3.密碼文件權(quán)限配置
chown nginx:nginx /opt/soft/.htpasswd
chmod 600 /opt/soft/.htpasswd
配置nginx
...
location /webdav {
client_max_body_size 5G; # 最大允許上傳文件大小
alias /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方法強(qiáng)制添加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擴(kuò)展支持的請求方法
create_full_put_path on; # 啟用創(chuàng)建目錄支持
dav_access group:rw all:r; # 創(chuàng)建文件的以及目錄的訪問權(quán)限
# auth_basic "Authorized Users Only";
# auth_basic "Authorized Users WebDAV";
auth_basic "user login";
auth_basic_user_file /opt/soft/.htpasswd;
}
...
根據(jù)自己需求,設(shè)置路徑趴生,以及文件存路徑
- 重啟Nginx
sytemctl restart nginx
客戶端
我這里使用的是windows阀趴,其實有很多工具
如:
Cyberduck: 官方地址: https://cyberduck.io/
我這里使用的是 RaiDrive: 官方地址: http://www.raidrive.com/
輸入自己對應(yīng)的配的配置,然后點擊確定 苍匆。此時打開我們的資源管理器(win + E)
此時我們就可以像使用本地文件一樣操作服務(wù)器上的文件了刘急。