# 1. 安装Nginx
apt install nginx -y
systemctl enable nginx
systemctl start nginx

# 2.下载配置webdav
wget https://github.com/hacdias/webdav/releases/download/v4.2.0/linux-amd64-webdav.tar.gz
tar -zxvf linux-amd64-webdav.tar.gz
cp webdav /usr/bin/

#3.配置webdav
# setting /opt/webdav.config.yml
echo "# Server related settings
address: 0.0.0.0
port: 10880
auth: true
tls: false
cert: cert.pem
key: key.pem
prefix: /
debug: false

# Default user settings (will be merged)
scope: .
modify: true
rules: []

# CORS configuration
cors:
  enabled: true
  credentials: true
  allowed_headers:
    - Depth
  allowed_hosts:
    - http://localhost:8080
  allowed_methods:
    - GET
  exposed_headers:
    - Content-Length
    - Content-Range

users:
  - username: admin
    password: admin
    scope: /mnt/md0/
    modify:   true
    rules:
      - regex: false
        allow: false
        path: /some/file
        modify: true" > /opt/webdav.config.yml

#4. 给webdav配置web访问
echo "server
{
    listen 80;
    server_name 172.16.11.6;
    index index.html index.htm default.htm default.html;
    root /home/bingo/;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header X-Real-IP ;
        proxy_set_header REMOTE-HOST ;
        proxy_set_header X-Forwarded-For ;
        proxy_set_header Host ;
        proxy_redirect off;
    }

    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log /dev/null;
        access_log /dev/null;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log /dev/null;
        access_log /dev/null;
    }
}" > /etc/nginx/conf.d/webdav.conf

# 5.重启nginx
systemctl restart nginx

# 6.配置webdav到systemd服务项
# seeting /usr/lib/systemd/system/webdav.service
echo "[Unit]
Description=WebDAV server
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/bin/webdav --config /opt/webdav.config.yml
Restart=on-failure

[Install]
WantedBy=multi-user.target" > /usr/lib/systemd/system/webdav.service

# 7.启动webdav
systemctl enable webdav
systemctl start webdav
systemctl status webdav
● webdav.service - WebDAV server
     Loaded: loaded (/lib/systemd/system/webdav.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-03-07 09:40:41 CST; 4h 32min ago
   Main PID: 63213 (webdav)
      Tasks: 9 (limit: 18972)
     Memory: 1.8G
        CPU: 1min 15.366s
     CGroup: /system.slice/webdav.service
             └─63213 /usr/bin/webdav --config /opt/webdav.config.yml



作者 uoscn