16

I'd like to bind the rails server to 127.0.0.1, instead of 0.0.0.0 so its not accessible when I'm working from coffee shops.

Is there a configuration file where I can specify this option so I don't have to pass the command line switch:

rails server -b 127.0.0.1

?

1

5 Answers 5

19

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?

7
  • The question is for the bound IP, not the port. I'm sure the config could be in the same file but it would be useful to specify the IP part here.
    – sjagr
    Commented Apr 29, 2015 at 14:01
  • Added the Host parameter. Commented Apr 29, 2015 at 14:02
  • This file is checked into version control, though, right? If the value is set here, then it's set in production as well.
    – Matt
    Commented Jan 29, 2016 at 15:36
  • Actually I don't know, but I assume you can set an 'if Rails.env...' in the file. Never tryed. Commented Jan 29, 2016 at 15:38
  • 3
    This not valid in rails 5 anymore. This worked for me: stackoverflow.com/a/33852354/520567 Commented Jul 28, 2016 at 7:21
4

You can make a bash script to just run the command by default:

#!/bin/bash
rails server -b 127.0.0.1

Put it in the same folder as your project, name it anything you want (e.g. devserv), then

chmod +x devserv

And all you have to do is ./devserv

3

I use Foreman as a process manager in development.

After adding gem 'foreman' to your Gemfile and running bundle install, create a file Procfile in the root of your application directory.

While you can add lines to manage other processes, mine just reads:

web: rails server -p $PORT -b 127.0.0.1

Then, to start the Rails server via the Procfile, run foreman start. If you have other processes here (Redis, workers) they'll boot at the same time.

1

If you're using puma, there are several ways:

  1. In config/puma.rb, add this:
bind 'tcp://localhost:3001'

However, this would cause puma to listen to two ports or more.

  1. In config/puma.rb, add this:
port ENV.fetch("PORT") { 3000 }, 'localhost'
  1. If you do not wish to sabotage your puma file, try modifying puma's default configuration in <puma_gem_path>/lib/puma/configuration.rb, and do this:
DefaultTCPHost = 'localhost'

Source Code

0

If you put the default options on config/boot.rb then all command attributes for rake and rails fails (example: rake -T or rails g model user)! So, 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:  '127.0.0.1', Port: 10524)
      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:  '127.0.0.1', Port: 10524)
      end
    end
  end
end

require 'rails/commands'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.