利用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM