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

使用rackup config.ru运行本地静态文件时遇两类Rack错误求助

解决Rack运行静态文件的两个错误

1. 修复uninitialized constant Rack::Static (NameError)

该错误是因为未加载Rack::Static模块,只需在config.ru最顶部添加引入语句:

require 'rack/static'

2. 修复Rack::Lint::LintError: uppercase character in header name

Rack 3.x版本开始严格遵循HTTP标准,要求响应头的键必须使用全小写格式,不能包含大写字母。你需要修改响应头的键名:

修改后的完整config.ru代码

require 'rack/static'

use Rack::Static,
  :urls => ["/images", "/js", "/css"],
  :root => "public"

run lambda { |env|
  [
    200,
    {
      'content-type'  => 'text/html',
      'cache-control' => 'public, max-age=86400'
    },
    File.open('public/index.html', File::RDONLY)
  ]
}

额外优化提示

如果后续遇到文件资源释放相关的警告,建议改用File.read返回字符串内容,避免文件对象未正确关闭的问题:

run lambda { |env|
  content = File.read('public/index.html')
  [
    200,
    {
      'content-type'  => 'text/html',
      'cache-control' => 'public, max-age=86400'
    },
    [content]
  ]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 01:43:24