Featured image of post Setting Up a WebDAV Service on Nginx

Setting Up a WebDAV Service on Nginx

Setting up WebDAV on Nginx is based on existing modules. For instructions on installing the WebDAV module, please refer to this article: https:…

Setting up a WebDAV service on Nginx builds upon existing modules. For instructions on installing the WebDAV module, please refer to this article: https://bmzhp.com/experience/215

To set up a WebDAV service on Nginx, follow these steps:

  1. Ensure that Nginx is installed and working properly.
  2. Create a WebDAV root directory and grant write permissions. For example, you can use the following commands:
1
2
mkdir /path/to/webdav
chown www-data:www-data /path/to/webdav

If you are using an LNMP environment, the user should be changed to www, which means you would use:

1
chown -R www:www /path/to/webdav
  1. Configure Nginx to enable the WebDAV module and specify the WebDAV root directory. Add the following code to your Nginx configuration file:
1
2
3
4
5
6
7
location /webdav {
    alias /path/to/webdav;
    client_body_temp_path /var/nginx/client_temp;
    dav_methods PUT DELETE MKCOL COPY MOVE;
    create_full_put_path on;
    dav_access user:rw group:rw all:r;
}

In the code above, “/webdav” is the URL path you want to map to the WebDAV service, while “/path/to/webdav” is the WebDAV root directory. Feel free to change these values as needed.

In Nginx, the client_body_temp_path directive specifies the temporary file storage path for receiving client request bodies. The WebDAV protocol allows for operations on large files, which can lead to excessive memory consumption during uploads or downloads. To prevent potential memory overflow issues, Nginx first caches large uploaded files to disk before writing them to the WebDAV root directory.

Therefore, it is generally necessary to set the client_body_temp_path directive when configuring the WebDAV service to specify the temporary file storage path on disk. This directive must be placed within the location block to ensure it only applies to the WebDAV service.

The WebDAV configuration provided above does not allow for anonymous access or file uploads.

In this code, the dav_access directive specifies that access to the WebDAV service requires authentication via a username and password. If you do not provide a username and password for the WebDAV service, you will not be able to access it or upload files.

Here’s an example configuration that allows for anonymous access (not recommended for production environments):

1
2
3
4
5
6
7
location /webdav {
    alias /path/to/webdav;
    client_body_temp_path /var/nginx/client_temp;
    dav_methods PUT DELETE MKCOL COPY MOVE;
    create_full_put_path on;
    dav_access group:rw all:r;
}

In this configuration, the parameters for dav_access are set to “group:rw all:r”, meaning anyone can access the WebDAV service in read-only mode, while only users from the assigned group can access it with read/write permissions.

Please note that allowing anonymous file uploads may pose security risks. It is advisable to enable anonymous upload permissions only when security can be guaranteed.

  1. Reload the Nginx configuration file to apply the changes:
1
sudo service nginx reload

Now, you have successfully deployed the WebDAV service on your Nginx server. You can connect to the WebDAV service and access the files using any client that supports the WebDAV protocol, such as Windows Explorer or Cyberduck. Please be aware that you will need to authenticate with a user account that has write permissions to upload or modify files.