[linux][nginx]nginx反向代理 webdav服務

反向代理一個完整 WebDAV 伺服器

# 假設後端 WebDAV 在 http://192.168.0.111:8080/dav/
server {
    listen 443 ssl;
    server_name dav.example.com;

    # ... SSL 設定略 ...

    client_max_body_size 0;           # 不限制上傳大小(視需求)
    proxy_http_version 1.1;           # WebDAV/長連線建議用 HTTP/1.1
    keepalive_requests 1000;

    location / {
        proxy_pass http://192.168.0.111:8080;

        # 讓反代更「透明」
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # 重要:保留 WebDAV 特有標頭(部分客戶端很依賴)
        proxy_set_header Destination   $http_destination;
        proxy_set_header Depth         $http_depth;
        proxy_set_header Overwrite     $http_overwrite;
        proxy_set_header If-None-Match $http_if_none_match;
        proxy_set_header If-Match      $http_if_match;
        proxy_set_header If-Modified-Since $http_if_modified_since;
        proxy_set_header If-Unmodified-Since $http_if_unmodified_since;
        proxy_set_header Lock-Token     $http_lock_token;
        proxy_set_header Timeout        $http_timeout;
        proxy_set_header Range          $http_range;
        proxy_set_header If-Range       $http_if_range;
        proxy_set_header Expect         $http_expect;  # 100-continue

        # 大檔/串流上傳(避免先寫到磁碟)
        proxy_request_buffering off;
        proxy_buffering off;

        # 某些 Windows WebDAV 客戶端相容性
        chunked_transfer_encoding off;  # 視後端/客戶端而定
    }
}

小重點

  • 不要用 limit_except GET { ... } 這類把非 GET/HEAD/POST 擋掉的設定。
  • 若前端做 Basic/Auth,確定與後端認證邏輯不互相衝突(雙重 401 會讓部分客戶端混亂)。
  • 如果需要讓後端知道真正的 Host/Proto,務必帶 X-Forwarded-*
  • 檔案很大或想節省 Nginx 磁碟 I/O,關 proxy_request_buffering
  • 某些客戶端(特別是 Windows WebClient)對 HTTPS、OPTIONSExpect: 100-continue、是否使用 chunked encoding 特別敏感,上面參數可改善相容性。

快速檢查清單

  • 後端 WebDAV 功能完整(含 LOCK/UNLOCK/PROPFIND)。
  • Nginx 沒擋 WebDAV 方法(未使用不當 limit_except)。
  • 保留並轉送 WebDAV 標頭(Destination/Depth/Overwrite/If-* 等)。
  • proxy_http_version 1.1、必要時關 proxy_request_buffering
  • 設好上傳大小(client_max_body_size)。
  • 與客戶端相容性調整(HTTPS、OPTIONS 回應、100-continue、chunked)。

結論

  • 要「完整轉送服務」:用 Nginx 當反代、把細節調好 → ✅ 沒問題。
  • 要「自己提供完整 WebDAV」只靠 Nginx 原生模組 → ❌ 不建議,功能不全。