Setup webdav on Apache and Nginx

Linux


Setup webdav with apache

[root@www ~]# mkdir /home/webdav
[root@www ~]# chown apache. /home/webdav
[root@www ~]# chmod 770 /home/webdav
[root@www ~]# vi /etc/httpd/conf.d/webdav.conf
# create new
<IfModule mod_dav_fs.c>
    DAVLockDB /var/lib/dav/lockdb
</IfModule>
Alias /webdav /home/webdav
<Location /webdav>
    DAV On
    SSLRequireSSL
    Options +Indexes
    AuthType Basic
    AuthName WebDAV
    AuthUserFile /etc/httpd/conf/.htpasswd
    <RequireAny>
        Require method GET POST OPTIONS
        Require valid-user
    </RequireAny>
</Location>

# add a user : create a new file with [-c]
[root@www ~]# htpasswd -c /etc/httpd/conf/.htpasswd rocky
New password:     # set password
Re-type new password:
Adding password for user rocky
[root@www ~]# systemctl restart httpd
# selinux if enabled
[root@www ~]# chcon -R -t httpd_sys_rw_content_t /home/webdav
[root@www ~]# semanage fcontext -a -t httpd_sys_rw_content_t /home/webdav

Setup webdav with nginx

Install
yum install yum-utils pcre-devel zlib-devel libxslt-devel libxml2-devel -y
wget http://nginx.org/download/nginx-1.16.1.tar.gz
git clone https://github.com/arut/nginx-dav-ext-module
tar -zvxf nginx-1.16.1.tar.gz && cd nginx-1.16.1
./configure --with-compat --with-http_dav_module --add-dynamic-module=../nginx-dav-ext-module/
# if nginx already installed, only make is ok, do not make install
make && make install
cp objs/ngx_http_dav_ext_module.so /etc/nginx/modules/
configure
server {
    listen 80;
    server_name webdav.example.com;
  
    location / {
        # 301 Only work for GET/HEAD/OPTIONS, So we use 307 to temporary for POST go through.
        if ($request_method = POST) {
            return 307 https://$host$request_uri;
        }
        return 301 https://$host$request_uri;
        }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name webdav.example.com;
    root /var/www/webdav/;
    index index.html index.htm;
    access_log /var/log/nginx/webdav/access.log combined gzip;
    error_log /var/log/nginx/webdav/error.log warn;
    ssl_certificate /etc/nginx/ssl/webdav.example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/webdav.example.com.key;
    ssl_dhparam /etc/nginx/ssl/dhparams.pem;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    add_header Strict-Transport-Security "max-age=31536000";
    add_header X-Content-Type-Options nosniff;

    location / {
        auth_basic "Restricted site.";
        auth_basic_user_file /etc/nginx/auth/webdav-users.passwd;
        client_body_temp_path /var/www/webdav/temp;
        dav_methods PUT DELETE MKCOL COPY MOVE;
        dav_ext_methods PROPFIND OPTIONS;
        create_full_put_path on;
        dav_access user:rw group:rw all:rw;
        autoindex on;
    }
}
system start nginx && systemctl enable nginx
nginx -V |grep webdav