How to Stop Rails' Built-In Server from Listening on 0.0.0.0 by Default

Is there a way to stop Rails' built-in server from listening on 0.0.0.0 by default?

You can update the /script/rails file in you rails app to reflect the following:

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)

# START NEW CODE
require "rails/commands/server"
module Rails
class Server
def default_options
super.merge({
:Host => 'my-host.com',
:Port => 3000,
:environment => (ENV['RAILS_ENV'] || "development").dup,
:daemonize => false,
:debugger => false,
:pid => File.expand_path("tmp/pids/server.pid"),
:config => File.expand_path("config.ru")
})
end
end
end
# END NEW CODE

require 'rails/commands'

This will bind the rails app to my-host.com when it starts up. You can still override the options from the command line.

I am not sure why this is not reflected in the Rails::Server API docs. You can have a look at https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb to see the server implementation.

Note that in Rails 4, the /script/rails file has been moved to /bin/rails.

Bind rails server to 127.0.0.1 by default

If you are searching for Rails 5:
Answer


In Rails ~> 4.0 you can customize the boot section of the Server class:

In /config/boot.rb add this lines:

require 'rails/commands/server'

module Rails
class Server
def default_options
super.merge({Port: 10524, Host: '127.0.0.1'})
end
end
end

As already answered on this questions:

How to change Rails 3 server default port in develoment?

How to change the default binding ip of Rails 4.2 development server?

What could be stopping rails from starting when using 'ASSET_HOST'

Asset_host just sets the url of where to look for "assets". Not sure why you want to change the ip of the rails server. I run rails in virtual box every day and just use the default. Just type rails s and then use localhost:3000 in your browser. Maybe you forgot to specify the port?

If you are really trying to change the IP of the rails server, this may help:

rails s -b <ip address>

From:

Is there a way to stop Rails' built-in server from listening on 0.0.0.0 by default?

rails server not responding to remote connections

Rails 4.2 changed the default IP address that rails server binds to, instead of binding to 0.0.0.0 (ie. all interfaces) it binds only to localhost.

Edit

Sorry, I see now that you made no mention of production, just a remote box. So I removed that bit from above.

To get around this you can do two things, either always start with the -b option:

rails s -b 0.0.0.0

Or, there's a little trick to add that to the default, see https://stackoverflow.com/a/8998401/152786 (note that you only need to merge in the :Port and :Host options, and you can set the :Host to 0.0.0.0 if you want).

What does it mean to run a local web server?

You're pretty much right on your understanding there. HTTP is just a text-based protocol that, like many, operates over TCP/IP.

The built-in WEBrick server isn't the best example of an HTTP server written in Ruby, but it's included for legacy reasons because it's often "good enough" to get you started. Pow is considerably better and despite being produced by the same company that produced Rails it's largely written in Node.

The beauty of HTTP, like a lot of internet based protocols, is it doesn't matter what language you use so long as you comply with the standard.

Rack is a layer that operates behind HTTP and provides a thin layer of abstraction on the request/response cycle.

Avoid display of backtrace when running Sinatra on an already taken port

This appears to be caused by an issue with Puma, that is fixed by this PR.

Ruby Sinatra Webservice running on localhost:4567 but not on IP

Following worked for me.

ruby app.rb -o 0.0.0.0



Related Topics



Leave a reply



Submit