VirtualBox 下 Ubuntu 11.10 上的 Rails 3.1 非常慢 [英] Rails 3.1 on Ubuntu 11.10 under VirtualBox very slow
问题描述
我在 Windows7 上安装了带有 Ubuntu 11.10 的 VirtualBox.我在 webrick (rails s) 上运行 Rails 3.1 并从 Windows 上的浏览器转到 VirtualBox 的 IP 地址 (192.168.2.xxx:3000).此时我遇到了麻烦 - 页面加载非常缓慢,在 Rails 控制台上,我看到它响应文件(css、js、图像)的速度有多慢:每个最多 5 秒!但是:如果我在 Ubuntu 中使用 0.0.0.0:3000 - 它运行完美.
I have VirtualBox with Ubuntu 11.10 on Windows7. I run Rails 3.1 on webrick (rails s) and go to VirtualBox's IP adress (192.168.2.xxx:3000) from browser on Windows. At this moment I face troubles - page loads very very slowly, on Rails console i see how slowly it responses files (css, js, images): up to 5 seconds for each! But: if I go 0.0.0.0:3000 inside Ubuntu - it works perfect.
问题出在哪里?在哪里寻找解决方案?
Where is the problem? Where to look for a solution?
推荐答案
如上所述,这似乎是另一个问题的重复,尽管该问题特别提到了 webrick 和远程桌面连接,但我都没有寻找,因为我不知道它特定于 webrick.
As mentioned above, it appears this is a duplicate of another issue, though that issue specifically mentions webrick and a remote desktop connection, neither of which was I looking for, as I didn't know it was specific to webrick.
所以我认为应该注意的是,这不是虚拟机或 BIOS 设置或类似的配置错误.这严格是一个 webrick 问题.如果其他服务不尝试进行反向 DNS 查找,它们就可以正常工作.
So I think it should be noted that this is NOT a misconfiguration of the virtual machine or BIOS settings or anything like that. This is strictly a webrick issue. Other services work just fine if they aren't trying to do reverse DNS lookups.
问题源于 Webrick 的默认设置,即尝试进行反向 DNS 查找,当查找失败时会出现问题,因为它往往在我的本地机器上对我来说始终如一.
The problem stems from Webrick's default setting to try and do a reverse DNS lookup, which has issues when the lookup fails, as it tends to do consistently for me on my local machine.
有几种方法可以解决这个问题.
There are a few ways to fix this.
首先是破解 /etc/hosts
以便您的客户端机器有一个条目.破解 /etc/hosts
是半高级的,但基本上相当于弄清楚你的主机的 IP 地址是什么,并在你的 /etc/hosts
文件中添加一行.这需要根级权限.
The first is to hack /etc/hosts
so your client machine has an entry. Hacking /etc/hosts
is semi-advanced, but basically amounts to figuring out what you host machine's IP address is and adding a line to your /etc/hosts
file. This requires root-level permissions.
- 获取您的 IP 地址 - 当您发出请求时,这将在 Rails 控制台中看到,看起来像这样:
Started GET "/";对于 2012 年 8 月 21 日星期二 11:33:23 -0700 的 10.0.2.2
- 在这种情况下,IP 地址为 10.0.2.2. - 在
/etc/hosts
中添加一行以标识该 IP 地址:10.0.2.2 Nerdmaster
- Get your IP address - this will be seen in the Rails console when you make a request, and will look something like this:
Started GET "/" for 10.0.2.2 at Tue Aug 21 11:33:23 -0700 2012
- in this case, the IP address is 10.0.2.2. - Add a line to
/etc/hosts
to identify that IP address:10.0.2.2 Nerdmaster
一切都会好起来的!
这似乎适用于一些 ubuntu 用户:
This seemed to work for some ubuntu users:
服务 avahi-daemon 停止
鉴于您正在终止一项服务,可能还有其他应用存在问题.这可能是最好的临时措施,而不是永久措施,但我对这项服务知之甚少,因此请避免相信我的话:)
Given you're killing a service, there may be other apps which have issues. This is probably best as a temporary measure, not a permanent one, but I know very little about the service, so avoid taking my word for this one :)
这也需要根级权限.
我真的很讨厌在每次更新时都必须重新编写核心 Ruby 代码,但这是很多人所做的:
I really despise hacking core Ruby code that I have to re-hack on every update, but this is what a lot of people do:
- 找到您的
webrick/config.rb
- 如果您运行的是普通 Ruby,它可能在
/usr/lib/ruby/[version]/webrick/config.rb
中 - 如果您使用 RVM,则必须找到合适的 ruby 目录,例如
/home/username/.rvm/rubies/[version]/lib/ruby/[version]/webrick/config.rb
- 我不知道 jack 关于 rbenv 等其他选项
- 最坏的情况,尝试
find/-type d -name "webrick"
- 查找
:DoNotReverseLookup =>无
. - 将
nil
改为true
. - 如果您没有看到此设置,则可能需要使用上面的
/etc/hosts
hack.
- Look for
:DoNotReverseLookup => nil
. - Change
nil
totrue
. - If you don't see this setting, you may have to use the
/etc/hosts
hack above.
如果您不使用 rvm 或类似的东西,这将需要根级权限.
If you aren't using rvm or something similar, this will require root-level permissions.
我不认为这是一个真正的解决方案,因为 webrick 有时是您最好(或至少最快)的选择,但您可以尝试不同的服务器.Thin 和 mongrel 似乎有很好的支持,根据其他有此问题的人所说,似乎没有进行反向 DNS 查找.我没有尝试过这些方法,所以我不确定它们有多好.
I don't think this is a real solution, as webrick is sometimes your best (or at least quickest) option, but you can try a different server. Thin and mongrel seem to have good support and, based on what others with this problem are saying, appear not to do a reverse DNS lookup. I haven't tried these approaches, so I don't know for sure how good they are.
无需根级权限即可完成此操作.
This can be done without root-level permissions.
这篇关于VirtualBox 下 Ubuntu 11.10 上的 Rails 3.1 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 如果您运行的是普通 Ruby,它可能在