Rails 4.2 Server port forwarding on Vagrant does not work
Solution 1
Rails 4.2 now binds to 127.0.0.1
by default and not 0.0.0.0
.
Start the server using bin/rails server -b 0.0.0.0
and that should sort it.
Solution 2
To run on a specific port :
rails server -b 0.0.0.0 -p 8520
Solution 3
Use:
rails s -b 0.0.0.0
or
Add to config/boot.rb
:
require 'rails/commands/server'
module Rails
class Server
new_defaults = Module.new do
def default_options
default_host = Rails.env == 'development' ? '0.0.0.0' : '127.0.0.1'
super.merge( Host: default_host )
end
end
# Note: Module#prepend requires Ruby 2.0 or later
prepend new_defaults
end
end
and work with rails s
Related videos on Youtube
Victor Leal
Updated on March 24, 2020Comments
-
Victor Leal about 4 years
I have a Vagrant VM with Rails installed with a sample app. The VM is configured to forward the port 3000 (of Rails Webrick server) to my host 3000 port.
config.vm.network "forwarded_port", guest: 3000, host: 3000
Everything is configured as seen in a lot of examples.
But, when I try to access
http://localhost:3000
nothing happens. I've also tried to forward to other random ports like 8081, 25600, without success. Doing a curl request also does not get anything (just a Connection reset by the peer message), and a curl request inside VM works perfectly (as expected).Both my PC and my VM runs Ubuntu 12.04. I'm using Ruby 2.2.0 and Rails 4.2.0.
An important point is that Apache works normally. I forwarded the port 80 to port 8080 and everything works. It seems that the problem is just with the Rails server, even if when I use other ports (
rails server -p 4000
, for example) -
Victor Leal over 9 yearsI'm gonna do it! Every server should use the 0.0.0.0 when inside a VM? I didn't know about that...
-
Max Woolf over 9 yearsNo, this isn't the most secure way to do it. 0.0.0.0 just means listen for any address. The change means that it will only respond to requests from the VM, not your host machine. It's fine for development, do not do it in production.
-
helsont over 9 yearsI've encountered the same problem. What recommendations do you have for setting up the production environment? Is there a specific IP rails should bind to?
-
Max Woolf over 9 yearsYou shouldn't be using webbrick in production, it's only designed for development.
-
Jonas Lomholdt almost 8 yearsThanks! Has this problem using PHP Cake 3.X, and this solved it!
-
StringsOnFire over 7 yearsYour second solution is great, thanks! Working with Rails 5.0.1
-
rnpd over 6 yearsThanks! Had this issue using Symphony and this solved i!