Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 on Ubuntu 11.10 under VirtualBox very slow

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?

like image 298
VitalyP Avatar asked Feb 23 '23 00:02

VitalyP


2 Answers

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.

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.

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.

Hack /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.

  • 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

All should be well!

Disable daemon

This seemed to work for some ubuntu users:

service avahi-daemon stop

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 :)

This also requires root-level permissions.

Hack webrick

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:

  • Find your webrick/config.rb
    • It may be in /usr/lib/ruby/[version]/webrick/config.rb if you're running a normal Ruby
    • If you use RVM, you'll have to find the appropriate ruby directory, e.g. /home/username/.rvm/rubies/[version]/lib/ruby/[version]/webrick/config.rb
    • I don't know jack about other options like rbenv
    • Worst-case scenario, try find / -type d -name "webrick"
  • Edit in your favorite editor (obviously this would be vim)
    • Look for :DoNotReverseLookup => nil.
    • Change nil to true.
    • If you don't see this setting, you may have to use the /etc/hosts hack above.

If you aren't using rvm or something similar, this will require root-level permissions.

Don't use webrick

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.

like image 60
Nerdmaster Avatar answered Mar 04 '23 13:03

Nerdmaster


Edit your Gemfile to add:

gem 'mongrel'

bundle install

rails s -> will use mongrel instead of webrick and be lightning fast.
like image 32
Scott Avatar answered Mar 04 '23 15:03

Scott