Wednesday | 28 FEB 2024
[ previous ]
[ next ]

Nginx and WebDAV Extended

Title:
Date: 2023-01-25
Tags:  

Nginx comes with limited webdav support in the form of the --with-http_dav_module flag.

You can check for this flag by looking at the nginx version information.

nginx -V 2>&1 | grep dav

This will give support for the basic functions like PUT, DELETE, MKCOL, COPY and MOVE. These correspond to creating documents, deleting documents, creating collections, copying and moving.

To get support for the PROPFIND and OPTIONS attributes which will give us access to directory listings, we'll need to add the nginx module nginx-dav-ext-module.

Luckily if you have a relatively modern version of nginx, we can build the module and link it dynamically.

The version step is to find out what version of nginx you are running and to get the tarball for that version.

wget https://nginx.org/download/nginx-1.20.1.tar.gz
tar xvf nginx-1.20.1.tar.gz

Now we can also download the webdav extended module:

git clone https://github.com/arut/nginx-dav-ext-module.git

Now we can cd into the nginx source folder that we extracted.

cd nginx-1.20.1

Now to configure the build script:

./configure --with-compat --with-http_dav_module --add-dynamic-module=../nginx-dav-ext-module/

We need the compat flag to make sure that the object code we generate is compatible.

Once we have configured the build, we can run make modules, this will create the dynamic modules which we will then copy to the modules folder.

make modules

Once the module is built, we can then copy it to the nginx module folder:

sudo cp objs/ngx_http_dav_ext_module.so /usr/lib64/nginx/modules

This may be different on your system so check the module-path flag in nginx -V. This will tell you where the modules are located.

Now the final thing we need to do is load our module into nginx. This path should match the above.

Update the /etc/nginx/nginx.conf file:

include /usr/share/nginx/modules/*.conf;
load_module /usr/lib64/nginx/modules/ngx_http_dav_ext_module.so;

Now we can update our webdav configuration in nginx:

    location /webdav {
        dav_methods PUT DELETE MKCOL COPY MOVE;
        dav_ext_methods PROPFIND OPTIONS;
    }

Here we add the PROPFIND and OPTIONS flags and specify the module we just built.

We can now run nginx -t and make sure that this configuration we made is valid. If the test passes, we are good to go!

We can restart nginx, sudo service nginx restart and then we can check if our webdav folder is listing things now:

curl -X PROPFIND 'http://example.com/webdav' -H "Depth: 1"

This should give a listing of files in the webdav folder.

With that we now have full webdav support in nginx!

Compiling and adding a module to nginx is a bit involved but I'm grateful that it is as simple as it is. Building just the module and dynamically loading is brilliant, I had thought I was going to need to rebuild nginx from source and then replace my existing nginx install. Whew!