dushaoshuai / dushaoshuai.github.io

https://www.shuai.host
0 stars 0 forks source link

Ubuntu 上使用 Nginx 配置一个简单的 webdav 服务 #143

Open dushaoshuai opened 5 months ago

dushaoshuai commented 5 months ago

WebDAV (Web Distributed Authoring and Versioning) 是一个 HTTP/1.1 的扩展,可以被认为是一个协议,它提供了通过 HTTP 进行文件传输的能力。

本文关注如何在 ubuntu 上使用 nginx 部署一个简单的 WebDAV 服务。

安装 nginx

$ sudo apt update
$ sudo apt install nginx

安装的 nginx 编译了 WebDAV 的支持模块 ngx_http_dav_module(在 nginx -V 的输出中可以看到 --with-http_dav_module 选项),这个模块支持 PUTDELETEMKCOLCOPYMOVE 方法。

安装扩展模块 libnginx-mod-http-dav-ext

$ sudo apt install libnginx-mod-http-dav-ext

这个扩展模块实现了 PROPFINDOPTIONS 方法,提供完整的 WebDAV 支持。

配置 HTTPS

使用 moz://a SSL Configuration Generator 生成一个 SSL 配置。

将生成的内容保存到文件 /etc/nginx/sites-available/webdav 中:

$ sudo cat <<EOF >/etc/nginx/sites-available/webdav
# generated 2024-04-14, Mozilla Guideline v5.7, nginx 1.18.0, OpenSSL 3.0.2, intermediate configuration
# https://ssl-config.mozilla.org/#server=nginx&version=1.18.0&config=intermediate&openssl=3.0.2&guideline=5.7
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    ssl_certificate /path/to/signed_cert_plus_intermediates;
    ssl_certificate_key /path/to/private_key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
    ssl_session_tickets off;

    # curl https://ssl-config.mozilla.org/ffdhe2048.txt > /path/to/dhparam
    ssl_dhparam /path/to/dhparam;

    # intermediate configuration
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305;
    ssl_prefer_server_ciphers off;

    # HSTS (ngx_http_headers_module is required) (63072000 seconds)
    add_header Strict-Transport-Security "max-age=63072000" always;

    # OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;

    # verify chain of trust of OCSP response using Root CA and Intermediate certs
    ssl_trusted_certificate /path/to/root_CA_cert_plus_intermediates;

    # replace with the IP address of your resolver
    resolver 127.0.0.1;
}
EOF

配置文件中剩余的部分,比如证书,比如域名 server_name your_domain_name;

配置基本认证服务

安装 apache2-utils

sudo apt-get update
sudo apt-get install apache2-utils

创建新的密码文件并添加第一个用户:

sudo htpasswd -c /etc/nginx/.htpasswd your_user_name

根据提示输入密码。

如果后续需要再添加或修改用户,不要使用 -c 选项,因为它会覆盖已有的密码文件:

sudo htpasswd /etc/nginx/.htpasswd your_user_name

配置 WebDAV

确定一个目录,将该目录作为 WebDAV 的根目录,假设是 /path/to/your/webdav

因为 nginx 的 worker 进程是以 www-data 用户的身份运行的,因此需要让 www-data 用户对 /path/to/your/webdav 目录有 rwx 权限,并且对其父目录有 x 权限。具体怎么做就不多说了。

编辑文件 /etc/nginx/sites-available/webdav,在 HTTPS server 块中添加以下内容:

location / {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/.htpasswd;

        if ($remote_user = "your_user_name") {
            root "/path/to/your/webdav/";
        }

        dav_methods PUT DELETE MKCOL COPY MOVE;
        dav_ext_methods PROPFIND OPTIONS;
        create_full_put_path on;
        dav_access user:rw group:rw;
        client_max_body_size 0;
        client_body_temp_path /var/tmp;
}

关于各指令的作用,详见 Module ngx_http_dav_module

重启 nginx

在 /etc/nginx/sites-enabled 目录下创建文件 /etc/nginx/sites-available/webdav 的软连接:

$ sudo ln -s /etc/nginx/sites-available/webdav /etc/nginx/sites-enabled/

检查配置文件是否正确:

sudo nginx -t

没问题的话重启 nginx:

$ sudo systemctl restart nginx

连接

这里使用 KDE dolphin 作为 WebDAV 客户端。

依次点击 Network -> Add Network Folder -> WebFolder (webdav) -> Next。

dolphin_webdav1

依次输入连接名(起描述作用,随便写),服务器域名,端口号,目录路径,勾选 Use encryption,点击 Save & Connect。

dolphin_webdav2

空白处右键,点击 Add to Places,将 WebFolder 添加到 KDE 资源管理器中。

dolphin_webdav3

See also