You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NGINX WebDav MKCOL请求返回405且mkdir()报错17:File exists排查

问题原因

你当前配置中location /块下的try_files $uri $uri/ /;是导致MKCOL异常的核心原因:
当你发起MKCOL /asd/请求时,NGINX首先执行try_files逻辑:

  1. 检查/asd/对应的本地目录/mnt/sd1c/documents/asd/不存在
  2. 触发最后一个回退规则/,执行内部重定向,将MKCOL请求作用到站点根路径/
  3. 此时dav模块尝试创建根路径对应的本地目录/mnt/sd1c/documents,该目录已存在,因此抛出mkdir() failed (17: File exists)错误,最终返回405状态码。

解决方案

修改example.com配置中location /块的内容,在执行try_files前拦截WebDAV写请求,跳过try_files逻辑:

location / {
    dav_methods PUT DELETE COPY MOVE MKCOL;
    # 新增以下配置:非GET/HEAD请求直接处理,不执行后续try_files
    if ($request_method !~ ^(GET|HEAD)$ ) {
        break;
    }
    autoindex on;
    autoindex_format xml;
    xslt_string_param path $uri;
    xslt_stylesheet /mnt/sd1c/www/example.com/internal/autoindex_format.xslt
        is_ssl_client_verify="$ssl_client_verify"
        val_remote_user="$remote_user";
    auth_basic $ssl_client_verify_auth;
    auth_basic_user_file /mnt/sd1c/www/example.com/passwd/documents;
    satisfy any;
    default_type text/plain;
    # 可按需改回原来的try_files规则,避免根路径重定向的副作用
    try_files $uri $uri/ =404;
}

验证步骤

修改完成后重载NGINX配置,执行以下命令测试:

$ curl -nkfX MKCOL https://example.com/asd/

创建成功会返回201状态码;如果目标目录已存在返回405属于正常逻辑,可在自动化脚本中先通过HEAD请求判断目录是否存在再执行MKCOL操作。

内容的提问来源于stack exchange,提问作者trBlueJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 19:24:05