Programming
How to Change Rails 4 Default Binding IP
January 28, 2017
0

Change Rails 4 default binding IP

 

Ruby on Rails 4A couple of days ago, while upgrading company’s platform from Rails 3.2.22.2 to Rails 4.2.7, I noticed that the default IP was bound to localhost.  I couldn’t then remotely access the application running on my development virtual machine.

This is due to a change in Rack for security reasons, as explained here.

I had then to figure out a way to access the application from outside the virtual machine, mainly because I usually code and test the application remotely and after some googling I found a nice and clean solution on Stack Overflow:

Open your boot.rb file in your preferred editor and add this snippet to the beginning of the file:

require 'rails/commands/server'

module Rails
    class Server
        def default_options
            super.merge(Host: '0.0.0.0', Port: 3000)
        end
    end
end

In this way you will add the “any host” option to Rack default options.
Be careful that, in this way, you could potentially add a security flaw to your application, as well explained on Rack github page:

Running Rack apps on 0.0.0.0 in development mode will allow malicious users on the local network (ex: a Coffee Shop or a Conference) to abuse or potentially exploit the app. Safer to default host to localhost when in development mode.

My advice is to use this workaround only when you’re on a quite secure network such as your company network or a VPN.

Enjoy!

Leave a Reply

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close