一、编译增加 nginx-dav-ext-module

1、查看 Nginx 版本并在官网下载对应源码包:

nginx -V

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xvzf nginx-1.18.0.tar.gz

2、下载 nginx-dav-ext-module 和 headers-more-nginx-module:

cd nginx-1.18.0
apt install git
git clone https://github.com/arut/nginx-dav-ext-module.git
git clone https://github.com/openresty/headers-more-nginx-module.git

3、重新编译 Nginx:

# 安装依赖及编译工具
apt install libpcre3-dev openssl libssl-dev libxslt-dev libgd2-xpm-dev libgeoip-dev gcc g++ make

# 复制 nginx -V 命令得到的 configure arguments
./configure --with-cc-opt='...' --add-module=./nginx-dav-ext-module --add-module=./headers-more-nginx-module
make

编译后的 Nginx 二进制文件在 objs 目录下,复制到 /usr/sbin 覆盖即可

二、配置 WebDAV

nano /etc/nginx/conf.d/webdav.conf

server {
    listen 8080;
    server_name 0.0.0.0;
    charset utf-8,gb18030,gbk;
    # 要共享的文件夹
    root /home;
    # 打开目录浏览
    autoindex on;

    location / {
        set $dest $http_destination;
        if (-d $request_filename) {
            rewrite ^(.*[^/])$ $1/;
            set $dest $dest/;
        }
        if ($request_method ~ (MOVE|COPY)) {
            more_set_input_headers 'Destination: $dest';
        }
        if ($request_method ~ MKCOL) {
            rewrite ^(.*[^/])$ $1/ break;
        }
        
        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;
        auth_basic "Authorized Users Only";
        auth_basic_user_file /etc/nginx/conf.d/webdav_htpasswd;
    }
}

最后重启 Nginx:systemctl restart nginx

本文参考:blog.ixk.me/recompilation-of-nginx-installed-for-apt-mode-adds-webdav.html,做部分修改

Last modification:July 12th, 2020 at 09:08 am
如果觉得我的文章对你有用,请随意赞赏