蓝色大大 发表于 2026-2-11 11:43:55

如何优雅的修改.well-known响应头里的content-type?

为了能快速了解并处理您的问题,请提供以下基础信息:面板、插件版本:V11.5.0
系统版本:Debian 11
问题描述:苹果association验证和安卓applink注册需要验证文件,但是响应头里的Content-Type必须是application/json,目前创建网站后会自动加载一个conf用于验证.well-known/目录下的文件及规则,苹果安卓都是放到这个目录下,目前这个脚本会强制覆盖Content-Type为text/plain。我临时把代码修改成下面这样解决问题了,请问有更优雅的办法处理吗?
set $well_known '';
if ( $uri ~ "^/.well-known/" ) {
set_by_lua_block $well_known {
    -- 获取路径
    local m,err = ngx.re.match(ngx.var.uri,"/.well-known/(.*)","isjo")
    -- 如果路径匹配
    if m then
      -- 拼接文件路径
      local filename = ngx.var.document_root .. m
      -- 判断文件路径中是否合法
      if not ngx.re.find(m,"\\./","isjo") then
      -- 判断文件是否存在
      local is_exists = io.open(filename, "r")
      if not is_exists then
            -- Java项目?
            filename = "/www/wwwroot/java_node_ssl" ..m
      end
      -- 释放
      if is_exists then is_exists:close() end
      -- 读取文件
      local fp = io.open(filename,'r')
      if fp then
          local file_body = fp:read("*a")
          fp:close()
          if file_body then
            ----------------------------------------------------------
            -- 修改开始:判断后缀名并设置对应的 Content-Type
            ----------------------------------------------------------
            local fname = m:lower() -- 转换为小写匹配
            if fname:find("%.json[      DISCUZ_CODE_0      ]quot;) or fname == "apple-app-site-association" then
                ngx.header['content-type'] = 'application/json'
            else
                ngx.header['content-type'] = 'text/plain'
            end
            ----------------------------------------------------------
            -- 修改结束
            ----------------------------------------------------------
            return file_body
          end
      end
      end
    end
    return ""
}
}

if ( $well_known != "" ) {
return 200 $well_known;
}


页: [1]
查看完整版本: 如何优雅的修改.well-known响应头里的content-type?