Killerpixler
Killerpixler

Reputation: 4050

rails server not responding to remote connections

I have a rails server on a remote box (debian wheezy, ruby 2.2, rails 4.2) and the server does not respond when I try to connect to it in my local browser or curl. The request times out. However when I ssh into the box and wget localhost:3000 it gets me the root page perfectly. Any suggestions?

Upvotes: 3

Views: 2034

Answers (2)

phlegx
phlegx

Reputation: 2742

To bind the server without using command arguments you can append this to bin/rails after line require_relative '../config/boot' and the code is executed only for the rails server command:

if ARGV.first == 's' || ARGV.first == 'server'
  require 'rails/commands/server'
  module Rails
    class Server
      def default_options
        super.merge(Host:  '0.0.0.0', Port: 3000)
      end
    end
  end
end

The bin/rails file loks like this:

#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application',  __FILE__)
require_relative '../config/boot'

# Set default host and port to rails server
if ARGV.first == 's' || ARGV.first == 'server'
  require 'rails/commands/server'
  module Rails
    class Server
      def default_options
        super.merge(Host:  '0.0.0.0', Port: 3000)
      end
    end
  end
end

require 'rails/commands'

Upvotes: 0

smathy
smathy

Reputation: 27961

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

Upvotes: 6

Related Questions