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

?

Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130

5 Answers5

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?

Community
  • 1
  • 1
Jorge de los Santos
  • 4,583
  • 1
  • 17
  • 35
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

sjagr
  • 15,983
  • 5
  • 40
  • 67
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.

Matt
  • 435
  • 3
  • 9
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

kangkyu
  • 5,252
  • 3
  • 34
  • 36
Emilio.C
  • 21
  • 7
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'
phlegx
  • 2,618
  • 3
  • 35
  • 39