Rails 3.1 on Ubuntu 11.10 under VirtualBox very sl

2019-02-14 19:53发布

问题:

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?

回答1:

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.



回答2:

Edit your Gemfile to add:

gem 'mongrel'

bundle install

rails s -> will use mongrel instead of webrick and be lightning fast.


回答3:

For me the slowness of Rails 4 (1 second+ for each asset 304) on an Ubuntu guest VM turned out to be Virtualbox shared folders. Found that cause in this thread, moving away from shared folders fixed the speed issue for me.



回答4:

I am using windows10 home edition with 8 ram, 250g ssd, when I run Rails4.x (thin) on Virtual box with ubuntu server 16, I found the render time is extreamlly slow, mostly up to 4 - 5 seconds, finally I moved rails project from host shared folder to Virtualbox vm inside, it's become quite faster now.



回答5:

Try turning off your firewall or antivirus on Windows.



回答6:

I liked Scott's answer of just switching to mongrel, but if you're using a modern version of ruby (> 1.9.1), mongrel is no longer supported. (Install Mongrel in Ruby 1.9.3)

I switched over to thin - http://code.macournoyer.com/thin/

sudo gem install thin

thin start

and it's super snappy for me.