Thin Web Server加载Rails预编译资源返回0字节问题求助
Rails 7.0 Docker容器静态资源返回0字节排查思路
问题背景
我有一个部署在Docker容器中的Rails 7.0应用,用Sprockets和Esbuild打包资源为application.css与application.js,生成带摘要的文件public/assets/application-<hash>.css和public/assets/application-<hash>.js。容器内public/assets目录下的资源均有有效内容,但通过Rails内置的Thin Web Server向浏览器提供这些资源时,所有资源(包括CSS、JS及图片)均返回0字节。应用在Docker容器本地运行,此问题与Nginx无关。
Staging环境配置
# Settings specified here will take precedence over those in config/application.rb # Configures whether Rails should serve static files from the public directory. config.public_file_server.enabled = true # Configures whether all registered config.eager_load_namespaces should be loaded at compile-time. This includes your application, engines, Rails frameworks and any other registered namespace. config.eager_load = true # Code is not reloaded between requests config.cache_classes = true # Configures whether or not to show helpful development messages on error config.consider_all_requests_local = false # Enable fragment caching config.action_controller.perform_caching = true # Configures Rails itself to serve static files config.serve_static_files = false # Compress javascripts and css config.assets.compress = true # Specify whether or not to fall back to Sprockets if precompiled assets aren't loaded config.assets.compile = false # Generate digests for assets URLs. config.assets.digest = true # Send logs to Docker STDOUT config.logger = Logger.new('/proc/1/fd/1') # Doesn't actually send mail config.action_mailer.perform_deliveries = false # The production environment is meant for finished, "live" apps. # Code is not reloaded between requests config.cache_classes = true # Full error reports are disabled and caching is turned on config.consider_all_requests_local = false config.action_controller.perform_caching = true # Specifies the header that your server uses for sending files config.action_dispatch.x_sendfile_header = "X-Sendfile" # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation can not be found) config.i18n.fallbacks = true # Send deprecation notices to registered listeners config.active_support.deprecation = :notify config.action_mailer.raise_delivery_errors = false
排查思路
- 修复静态文件服务配置冲突:Rails 5之后
config.serve_static_files已被config.public_file_server.enabled替代,当前配置中两者共存(enabled=true但serve_static_files=false)可能引发异常。建议删除config.serve_static_files = false,仅保留config.public_file_server.enabled = true。 - 移除不兼容的X-Sendfile配置:Thin服务器不支持
X-Sendfile头,当前设置config.action_dispatch.x_sendfile_header = "X-Sendfile"会导致Rails尝试让服务器直接发送文件,但Thin无法处理,最终返回空内容。需将该行注释或设为nil。 - 验证资产预编译结果:进入Docker容器,执行
ls -lh public/assets确认文件大小正常,用cat public/assets/application-<hash>.css查看内容是否完整(压缩后的代码有有效内容即可)。若文件为空,重新执行RAILS_ENV=staging rails assets:precompile确保预编译无错误。 - 查看Rails请求日志:访问静态资源时查看容器日志,检查是否有文件找不到、权限错误或IO异常提示。可临时将
config.consider_all_requests_local设为true获取详细错误信息,排查后改回。 - 修复文件权限:确保Docker容器内
public/assets目录及文件的权限允许Rails运行用户读取。执行chmod -R 644 public/assets和chown -R rails:rails public/assets(根据实际运行用户调整)。 - 测试Thin静态文件服务能力:在容器内
public目录创建test.txt并写入内容,访问http://<容器IP>:<端口>/test.txt,若仍返回0字节,说明Thin自身静态服务有问题,尝试重启Thin或更换为Puma测试。
内容的提问来源于stack exchange,提问作者Thomas
相关产品推荐
相关产品推荐

