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

本地无法以生产模式启动Rails应用,请求排查403访问错误

Rails生产模式访问localhost出现403权限错误的解决方法

你的403错误和终端提示的Blocked hosts: localhost:3000,核心原因是Rails生产环境默认启用了**主机授权(Host Authorization)**安全机制,用来防范DNS绑定攻击,但你的config/environments/production.rb里没把localhost加入允许访问的主机列表。

解决步骤:

  1. 打开config/environments/production.rb配置文件,找到被注释掉的config.hosts段落,取消注释并添加localhost相关规则:

    # Enable DNS rebinding protection and other `Host` header attacks.
    config.hosts = [
      "example.com",     # 保留你实际生产环境的域名
      /.*\.example\.com/, # 保留子域名匹配规则(如果需要)
      "localhost",       # 允许localhost访问
      /localhost:\d+/    # 可选:匹配localhost加任意端口,避免端口变化后再次报错
    ]
    

    如果只是本地临时调试生产环境,也可以临时关闭主机授权(仅用于本地测试,正式生产环境禁止这么做):

    config.hosts = nil
    
  2. 重启Rails生产服务器:

    bin/rails s -e production
    

重新访问http://localhost:3000就能正常打开应用了。

注:开发环境默认允许所有主机访问,所以不会触发这个限制,生产环境出于安全考虑强制要求配置允许的主机列表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 16:25:19