1、简介

  废话可不看~

  WebDAV (Web-based Distributed Authoring and Versioning) 一种基于 HTTP 1.1协议的通信协议。它扩展了HTTP 1.1,在GET、POST、HEAD等几个HTTP标准方法以外添加了一些新的方法,使应用程序可对Web Server直接读写,并支持写文件锁定(Locking)及解锁(Unlock),还可以支持文件的版本控制。

  Nginx的轻量高效十分合我的胃口,但Nginx默认只实现了WebDAV的PUT、DELETE、MKCOL、COPY、MOVE,这显然不能满足日常的使用。所以,这里使用Nginx加载动态模块 nginx-dav-ext-module 来支持WebDAV额外需要的PROPFIND和OPTIONS方法。

  这里选择将nginx-dav-ext-module编译成动态库,这样可以不影响当前已安装的Nginx,只需要改一下配置文件并重载Nginx就可以生效了。

2、准备工作

2.1、安装编译环境和所需依赖

yum -y install gcc gcc-c++ make expat-devel 
yum -y install openssl-devel zlib-devel pcre-devel

2.2、下载nginx源码

wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -xzvf nginx-1.14.0.tar.gz

2.3、下载nginx-dav-ext-module源码

wget -O nginx-dav-ext-module.zip https://codeload.github.com/arut/nginx-dav-ext-module/zip/master
unzip nginx-dav-ext-module.zip
mv nginx-dav-ext-module nginx-1.14.0/nginx-dav-ext-module

3、编译源码

3.1、编译动态库(注①)

cd nginx-1.14.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 --add-dynamic-module=nginx-dav-ext-module-master/ --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'
make

3.2、配置动态库

  将得到的动态库文件ngx_http_dav_ext_module.zip移动到nginx模块目录下。

cp objs/ngx_http_dav_ext_module.so /etc/nginx/modules/ngx_http_dav_ext_module.so

  然后修改nginx主配置文件/etc/nginx/nginx.conf,在Server globals后面添加模块加载命令
"load_module modulePath"。

# Server globals
user                    nginx;
worker_processes        auto;
worker_rlimit_nofile    65535;
error_log               /var/log/nginx/error.log crit;
pid                     /var/run/nginx.pid;

load_module modules/ngx_http_dav_ext_module.so;

# Worker config
......

3.3、配置WebDav扩展

vim /etc/nginx/conf.d/webdav.conf
修改内容如下:

server {
    listen       80;
    server_name  webdav.cmdschool.org;
    access_log  /var/log/nginx/webdav.access.log  main;
    location / {
        root    /home/swapzone;
        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;
    }
}

3.4 重启服务

service nginx restart

3.5 安装并测试

然后使用webdav工具连接即可。


注①:

一开始,我按照nginx-dav-ext-module的说明进行编译

cd nginx-1.14.0
$ ./configure --with-http_dav_module --add-dynamic-module=nginx-dav-ext-module/
make

但编译出的库文件ngx_http_dav_ext_module_default.zip并不能使用,加载模块的时候会提示

nginx -t
nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_dav_ext_module.so" is not binary compatible in /etc/nginx/nginx.conf:8
nginx: configuration file /etc/nginx/nginx.conf test failed

这时使用nginx -V来确认当前版本启用的模块,即编译参数

nginx -V
nginx version: nginx/1.8.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --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-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --add-module=../nginx-dav-ext-module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

复制配置参数,并在后面增加

--add-dynamic-module=nginx-dav-ext-module/

然后重新编译

$ ./configure 刚才组合的参数
make

编译完成即可使用。