Is there a way to stop Rails' built-in server

2019-01-25 10:25发布

I do a lot of web development on untrusted networks (coffeeshops, the neighbors' open wifi, DEF CON), and I get twitchy when random, assuredly buggy software (my Rails app under development, say) binds a port on 0.0.0.0 and starts taking requests from all comers. I know that I can specify the address of binding with the -b option to the server, but I'd like to change the default globally so it always runs that way unless I tell it otherwise. Of course I can also run some kind of firewall which will block the connection, but better not to listen in the first place. Is there a '.railsrc' file or similar -- at least a per-project settings file, but preferably some global settings file -- which I can use to force the server to only bind to 127.0.0.1 by default?

3条回答
我想做一个坏孩纸
2楼-- · 2019-01-25 11:02

Use the --binding=ip parameter:

rails s --binding=127.0.0.1

https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb

查看更多
Ridiculous、
3楼-- · 2019-01-25 11:03

There's no way to change it globally, you'll have to use -b.

rails s -b <ip address>

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-25 11:05

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.

查看更多
登录 后发表回答