Thursday | 21 NOV 2024
[ previous ]
[ next ]

Nginx WebDAV

Title:
Date: 2023-01-25
Tags:  

WebDAV is an open protocol to manage files on a webserver using HTTP. It has the ability to create, delete and move documents using http requests like PUT and DELETE. Thanks to it being a web standard it comes baked into nginx.

We can use WebDAV to let us upload and manage files without having to write a specific backend. The end goal is to build an upload site with as much simplicity as possible. This project I have in mind is just for me so I'm not worried about scalability or doing things correctly.

You can confirm this by checking to see if nginx has the webdav module enabled. If not, you may need to compile build nginx with that module enabled. Luckily it should come by default.

nginx -V 2>&1 | grep dav

This should output:

configure arguments:... --with-http_dav_module ...

Once we make sure nginx has the webdav module, we can then create a webdav directory and set it up to be accessed using the webdav protocol.

server {
    listen 8081;
    server_name _;

    root /usr/share/nginx/html/website;

    location /webdav {
        dav_methods PUT DELETE MKCOL COPY MOVE;
        dav_access user:rw group:rw all:rw;
        create_full_put_path on;
        client_body_temp_path /tmp/upload_tmp;
    }
}

Once we have the configuration set, we can restart nginx.

sudo service nginx restart

In the main website directory, we can create a webdav folder. This folder will need to be readable and writeable by the nginx user.

Now we can try uploading a file.

curl -T file.txt http://localhost/webdav/

This will upload file.txt to the webdav folder and name if file.txt.

Voila! We now have a very simple way of uploading files.