Ruby-on-rails 如何在 Rails 上禁用“无法从...渲染控制台”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29417328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable "Cannot Render Console from..." on Rails
提问by Leandro Fran?a
I'm using Ubuntu/vagrant as my development environment. I'm getting these messages on rails console:
我使用 Ubuntu/vagrant 作为我的开发环境。我在 rails 控制台上收到这些消息:
Started GET "/assets/home-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for 10.0.2.2 at 2015-04-02 15:48:31 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Is it possible to disable those "cannot render..." messages or allow them in any way?
是否可以禁用那些“无法呈现...”消息或以任何方式允许它们?
回答by ydaetskcoR
You need to whitelist the 10.0.2.2 network space in the Web Console config.
您需要在 Web 控制台配置中将 10.0.2.2 网络空间列入白名单。
So you'll want something like this:
所以你会想要这样的东西:
class Application < Rails::Application
config.web_console.whitelisted_ips = '10.0.2.2'
end
Read herefor more information.
阅读此处了解更多信息。
As pointed outby pguardiario, this wants to go into config/environments/development.rbrather than config/application.rbso it is only applied in your development environment.
正如pguardiario所指出的,这想要进入而不是因此它仅适用于您的开发环境。config/environments/development.rbconfig/application.rb
回答by Flavio Wuensche
You can whitelist single IP's or whole networks.
您可以将单个 IP 或整个网络列入白名单。
Say you want to share your console with 192.168.0.100. You can do this:
假设您想与192.168.0.100. 你可以这样做:
class Application < Rails::Application
config.web_console.whitelisted_ips = '192.168.0.100'
end
If you want to whitelist the whole private network, you can do:
如果要将整个专用网络列入白名单,可以执行以下操作:
class Application < Rails::Application
config.web_console.whitelisted_ips = '192.168.0.0/16'
end
If you don't wanna see this message anymore, set this option to false:
如果您不想再看到此消息,请将此选项设置为 false:
class Application < Rails::Application
config.web_console.whiny_requests = false
end
Be careful what you wish for, 'cause you might just get it all
小心你想要的,因为你可能会得到一切
This is probably only for development purposes so you might prefer to place it under config/environments/development.rbinstead of config/application.rb.
这可能是只为开发目的,所以你可能更愿意将其下config/environments/development.rb的替代config/application.rb。
回答by Pak
Hardcoding an IP into a configuration file isn't good. What about other devs? What if the ip changes?
将 IP 硬编码到配置文件中并不好。其他开发者呢?ip变了怎么办?
Docker-related config should not leak into the rails app whenever possible. That's why you should use env vars in the config/environments/development.rbfile:
Docker 相关的配置不应该尽可能泄漏到 rails 应用程序中。这就是为什么你应该在config/environments/development.rb文件中使用 env vars 的原因:
class Application < Rails::Application
# Check if we use Docker to allow docker ip through web-console
if ENV['DOCKERIZED'] == 'true'
config.web_console.whitelisted_ips = ENV['DOCKER_HOST_IP']
end
end
You should set correct env vars in a .envfile, not tracked into version control.
您应该在.env文件中设置正确的环境变量,而不是跟踪到版本控制中。
In docker-compose.ymlyou can inject env vars from this file with env_file:
在docker-compose.yml你可以注入ENV从这个文件与瓦尔env_file:
app:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
links:
- db
environment:
- DOCKERIZED=true
env_file:
- ".env"
Based on the feebdack received in comments, we can also build a solution without environment variables:
根据评论中收到的反馈,我们也可以构建一个没有环境变量的解决方案:
class Application < Rails::Application
# Check if we use Docker to allow docker ip through web-console
if File.file?('/.dockerenv') == true
host_ip = `/sbin/ip route|awk '/default/ { print }'`.strip
config.web_console.whitelisted_ips << host_ip
end
end
I'll leave the solutions with env var for learning purposes.
出于学习目的,我将使用 env var 留下解决方案。
回答by Meta Lambda
Auto discovery within your config/development.rb
自动发现您的 config/development.rb
config.web_console.whitelisted_ips = Socket.ip_address_list.reduce([]) do |res, addrinfo|
addrinfo.ipv4? ? res << IPAddr.new(addrinfo.ip_address).mask(24) : res
end
Of course might need to add
当然可能需要添加
require 'socket'
require 'ipaddr'
Within your file.
在您的文件中。
回答by kwerle
Anyone on any of my private networks is welcome.
欢迎任何在我的任何私人网络上的人。
I run in a docker container and I don't care which network it wants to use this week.
我在一个 docker 容器中运行,我不在乎它本周想使用哪个网络。
config/environments/development.rb add line
config/environments/development.rb 添加行
config.web_console.whitelisted_ips = ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']
回答by Scymex
For development environment: Detect if it's docker, then determine the IP address and whitelist it
开发环境:检测是否是docker,然后确定IP地址并加入白名单
# config/environments/development.rb
require 'socket'
require 'ipaddr'
Rails.application.configure do
...
# When inside a docker container
if File.file?('/.dockerenv')
# Whitelist docker ip for web console
# Cannot render console from 172.27.0.1! Allowed networks: 127.0.0.1
Socket.ip_address_list.each do |addrinfo|
next unless addrinfo.ipv4?
next if addrinfo.ip_address == "127.0.0.1" # Already whitelisted
ip = IPAddr.new(addrinfo.ip_address).mask(24)
Logger.new(STDOUT).info "Adding #{ip.inspect} to config.web_console.whitelisted_ips"
config.web_console.whitelisted_ips << ip
end
end
end
For me this prints the following and the warning goes away
对我来说,这会打印以下内容并且警告消失
Adding 172.27.0.0 to config.web_console.whitelisted_ips
Adding 172.18.0.0 to config.web_console.whitelisted_ips
My solution was to combine
我的解决方案是结合
- the answer from user2481743 ?? https://stackoverflow.com/a/42142563/2037928
- the comment from jottr ?? How to disable "Cannot Render Console from..." on Rails
- 来自 user2481743 的回答 ?? https://stackoverflow.com/a/42142563/2037928
- 来自 jottr 的评论??如何在 Rails 上禁用“无法从...渲染控制台”
回答by Alexander Ryhlitsky
If you are using Docker most likely you don't want neither to introduce new ENV variables nor to hardcode your specific IP address.
如果您使用 Docker,您很可能既不想引入新的 ENV 变量,也不想硬编码您的特定 IP 地址。
Instead you may want to check that you are in Docker using /proc/1/cgroup, and to allow your host IP (both for web_consoleand better_errors). Add to your config/environments/development.rb
相反,您可能想使用 来检查您是否在 Docker 中/proc/1/cgroup,并允许您的主机 IP(用于web_console和better_errors)。添加到您的config/environments/development.rb
# https://stackoverflow.com/a/20012536/4862360
if File.read('/proc/1/cgroup').include?('docker')
# https://stackoverflow.com/a/24716645/4862360
host_ip = `/sbin/ip route|awk '/default/ { print }'`.strip
BetterErrors::Middleware.allow_ip!(host_ip) if defined?(BetterErrors::Middleware)
config.web_console.whitelisted_ips << host_ip
end
回答by Fabian Kübler
For me, whitelisted_ipsdidn't seem to work in a new project. The Readme states the corresponding configuration entry is supposed to be permissionsnow:
对我来说,whitelisted_ips似乎没有在新项目中工作。自述文件指出相应的配置条目现在应该是permissions:
Rails.application.configure do
config.web_console.permissions = '192.168.0.0/16'
end
https://github.com/rails/web-console/blob/master/README.markdown
https://github.com/rails/web-console/blob/master/README.markdown
回答by Dayvson Lima
class Application < Rails::Application
config.web_console.whitelisted_ips = %w( 0.0.0.0/0 ::/0 )
end
回答by x-yuri
If you run your site locally (on the host) it generally works out, since 127.0.0.1is always permitted. But if you're going to put your site into a container (not in production, locally), you might want to add this into config/environments/development.rb:
如果你(在主机上)在本地运行您的网站,一般的作品出来,因为127.0.0.1是总是被允许。但是,如果您要将站点放入容器中(不在生产中,在本地),则可能需要将其添加到config/environments/development.rb:
require 'socket'
require 'ipaddr'
Rails.application.configure do
...
config.web_console.permissions = Socket.getifaddrs
.select { |ifa| ifa.addr.ipv4_private? }
.map { |ifa| IPAddr.new(ifa.addr.ip_address + '/' + ifa.netmask.ip_address) }
...
end
P.S. Most of the time you want it to whine (don't want to do config.web_console.whiny_requests = false). Because it might mean you're running web-consolein production (which you shouldn't do).
PS 大多数时候你希望它发牢骚(不想做config.web_console.whiny_requests = false)。因为这可能意味着您正在web-console生产中运行(您不应该这样做)。

