1.1

利用Nginx WebDAV搭建自己的网盘

利用Nginx WebDAV搭建自己的网盘

需要准备的东西

硬件:

Linux服务器

软件:

名称 简介 下载地址
nginx源码 服务端需要自己编译代码 https://nginx.org/en/download.html
nginx扩展dav模块 nginx自身的dav功能不足以用来做webdav,需要用这个模块扩展 https://github.com/arut/nginx-dav-ext-module.git
raidrive windows客户端 https://www.raidrive.com/download

编译nginx

安装用到的库

apt install -y gcc make libpcre3-dev libssl-dev zlib1g-dev libxml2-dev libxslt-dev libgd-dev libgeoip-dev git

下载源码与扩展库

请在https://nginx.org/en/download.html找最新版源码.

wget 最新版源码下载地址
tar zxvf 源码压缩包
cd 源码目录
git clone https://github.com/arut/nginx-dav-ext-module.git

配置与编译安装Nginx

./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-module=./nginx-dav-ext-module

make -j8
make install

配置Nginx

echo "user root;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;
        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        ssl_prefer_server_ciphers on;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        gzip on;
        gzip_disable \"msie6\";
        include /etc/nginx/sites-enabled/*;
}">/etc/nginx/nginx.conf
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled
mkdir /var/lib/nginx/

配置Systemd让Nginx开机自动运行

mkdir -p /usr/lib/systemd/system
echo "[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target">/usr/lib/systemd/system/nginx.service
systemctl enable nginx
systemctl start nginx

配置Webdav

创建目录

这个目录将成为网盘的根目录

mkdir -p /mnt/webdav

配置密码

注意,这里改一下命令里的用户名和密码,后面连接要用

# 生成密码文件:
echo 用户名:$(openssl passwd -crypt 密码)>/etc/nginx/webdavpasswd

创建Nginx配置文件

注意修改里面的目录

echo "server {
              location / {
                              set \$dest \$http_destination;
                              if (-d \$request_filename) {
                                              rewrite ^(.*[^/])\$ \$1/;
                                              set \$dest \$dest/;
                              }
                              if (\$request_method ~ MKCOL) {
                                              rewrite ^(.*[^/])\$ \$1/ break;
                              }
                              root /mnt/webdav/;
                              autoindex on;
                              dav_methods PUT DELETE MKCOL COPY MOVE;
                              dav_ext_methods PROPFIND OPTIONS;
                              create_full_put_path on;
                              client_max_body_size 0M;
                              dav_access user:rw group:rw all:rw;
                              auth_basic \"Authorized Users Only\";
                              auth_basic_user_file /etc/nginx/webdavpasswd;
              }
              #server_name ;
              listen 80;
}">/etc/nginx/sites-available/webdav
ln -s /etc/nginx/sites-available/webdav /etc/nginx/sites-enabled/webdav

重启Nginx:

systemctl restart nginx

如果没有报错就是成功了

Windows客户端连接


填写这些:

注意address后面的勾,用于选择http协议或者https协议
apply -> ok

如果这里显示三角形,点一下,就会连接,然后显示正方形表示成功.

Linux客户端连接

安装

apt install -y davfs2
echo "use_locks       0" >> /etc/davfs2/davfs2.conf

手工挂载试试:

mkdir /mnt/drive1
mount -t davfs https://xx.com/dav/ /mnt/drive1

设为开机自动挂载

网上很多文章都推荐添加到fstab,不过不建议那么干,那玩意不稳定,很容易出问题,导致开机无法进系统,这里推荐用systemd管理挂载
复制这段脚本,参数分别修改一下,在shell执行,就可以配置好自动挂载

# -----编辑这些参数:
export url=https://xx.com/dav/
export mntpath=/mnt/drive1/
export mntscript=mnt-drive1
# 修改时注意上面两行目录名称的关联,如果不一致会挂载失败
export user=user
export password=password
# -----
# 下面指令自动生成挂载文件,别改
echo "\"$url\" \"$user\" \"$password\"" >> /etc/davfs2/secrets
export mountfile=/etc/systemd/system/$mntscript.mount
export automountfile=/etc/systemd/system/$mntscript.automount
echo "[Unit]
Description = Mount webdav disk
Wants=network.target
After=network.target
[Mount]
What = $url
Where = $mntpath
Type = davfs
Options = defaults
[Install]
WantedBy = multi-user.target">$mountfile

echo "[Automount]
Where = $mntpath
[Install]
WantedBy = multi-user.target">$automountfile
systemctl enable ${mntscript}.automount
systemctl start ${mntscript}.automount
posted @ 2020-07-31 15:45  asml  阅读(15673)  评论(1编辑  收藏  举报
@.@