宝塔10周年开年大促,年度钜惠,先领红包再下单,享受折上折,还有抽奖!查看活动
当前位置:论坛首页 > Linux面板 > 求助

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

发表在 Linux面板2026-2-11 11:43 [复制链接] 0 80

为了能快速了解并处理您的问题,请提供以下基础信息:
面板、插件版本:V11.5.0

系统版本:Debian 11

问题描述:苹果association验证和安卓applink注册需要验证文件,但是响应头里的Content-Type必须是application/json,目前创建网站后会自动加载一个conf用于验证.well-known/目录下的文件及规则,苹果安卓都是放到这个目录下,目前这个脚本会强制覆盖Content-Type为text/plain。我临时把代码修改成下面这样解决问题了,请问有更优雅的办法处理吗?
  1. set $well_known '';
  2. if ( $uri ~ "^/.well-known/" ) {
  3.   set_by_lua_block $well_known {
  4.     -- 获取路径
  5.     local m,err = ngx.re.match(ngx.var.uri,"/.well-known/(.*)","isjo")
  6.     -- 如果路径匹配
  7.     if m then
  8.       -- 拼接文件路径
  9.       local filename = ngx.var.document_root .. m[0]
  10.       -- 判断文件路径中是否合法
  11.       if not ngx.re.find(m[1],"\\./","isjo") then
  12.         -- 判断文件是否存在
  13.         local is_exists = io.open(filename, "r")
  14.         if not is_exists then
  15.             -- Java项目?
  16.             filename = "/www/wwwroot/java_node_ssl" ..  m[0]
  17.         end
  18.         -- 释放
  19.         if is_exists then is_exists:close() end
  20.         -- 读取文件
  21.         local fp = io.open(filename,'r')
  22.         if fp then
  23.           local file_body = fp:read("*a")
  24.           fp:close()
  25.           if file_body then
  26.             ----------------------------------------------------------
  27.             -- 修改开始:判断后缀名并设置对应的 Content-Type
  28.             ----------------------------------------------------------
  29.             local fname = m[1]:lower() -- 转换为小写匹配
  30.             if fname:find("%.json[        DISCUZ_CODE_0        ]quot;) or fname == "apple-app-site-association" then
  31.                 ngx.header['content-type'] = 'application/json'
  32.             else
  33.                 ngx.header['content-type'] = 'text/plain'
  34.             end
  35.             ----------------------------------------------------------
  36.             -- 修改结束
  37.             ----------------------------------------------------------
  38.             return file_body
  39.           end
  40.         end
  41.       end
  42.     end
  43.     return ""
  44.   }
  45. }

  46. if ( $well_known != "" ) {
  47.   return 200 $well_known;
  48. }
复制代码


使用道具 举报 只看该作者 回复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

紧急运维服务

响应时间:3分钟

问题处理方式:宝塔专家1对1服务

工作时间:工作日:9:00 - 18:30

宝塔专业团队为您解决服务器疑难问题

点击联系技术分析

工作时间:09:00至18:30

快速回复 返回顶部 返回列表