Rails 如何设置 rails console 中 app.host 的默认值?

Amosasas · April 23, 2020 · Last by Amosasas replied at April 24, 2020 · 3188 hits

Rails 6.0中:

rails console -e development或者rails console -e test想进行一些简单的调试,但是app.get一直是 403,查看 app.host 发现是 www.example.com,设置成 localhost 就行了,但是每次都要这样搞一遍很麻烦,这个东西可以用啥配置?我自己尝试往config/environments/xx.rb里加 config,但是https://guides.rubyonrails.org/configuring.html中并没有找到合适的。有大佬知道这个东西应该怎么设置吗?

Reply to zhuoerri

难过,但是我发现 rails console -e test 起的时候,app.host 虽然是 example.com 但是 app.get('/') 是不会 403 的,只有 development 的时候才有,这是因为 spec/下的 helper 的原因吗?

403 的原因应该跟 app.host 和 spec/下的helper都无关,你自己仔细检查下代码

Reply to zhuoerri

嗯 已经解决了。之前 Rails 5 一样的设置没用问题,查了一下应该是 Rails 6 新引了一个 Middleware,会去 check 请求的 hostname 避免 DNS 的重新绑定攻击,解决方法很简单,就是把名单清空config.hosts.clear,要么加上一条记录config.hosts << 'your_hostname' https://stackoverflow.com/questions/53878453/upgraded-rails-to-6-getting-blocked-host-error

Amosasas closed this topic. 24 Apr 09:45
You need to Sign in before reply, if you don't have an account, please Sign up first.