2011-12-12 56 views
9

我在不受信任的网络(咖啡店,邻居的开放WiFi,DEF CON)上进行了大量的网络开发,当随机,确实有问题的软件(我的Rails应用程序正在开发中)说0.0绑定一个端口时,我得到了twitchy。 0.0并开始接受所有来者的请求。我知道我可以使用-b选项指定服务器的绑定地址,但是我想全局更改默认值,所以它总是以这种方式运行,除非我另有说明。当然,我也可以运行某种防火墙,阻止连接,但最好不要听。是否有'.railsrc'文件或类似的文件 - 至少每个项目的设置文件,但最好是一些全局设置文件 - 我可以使用它来强制服务器默认绑定到127.0.0.1?有没有办法阻止Rails的内置服务器默认侦听0.0.0.0?

+2

如果您的操作系统支持别名,请使用它来运行服务器。 –

回答

4

您可以更新/脚本/轨在你的文件的Rails应用程序,以反映如下:

#!/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' 

这将绑定轨道应用程序到我的-host.com启动时。您仍然可以覆盖命令行中的选项。

我不确定为什么这不会反映在Rails :: Server API文档中。你可以看看https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb来查看服务器的实现。

请注意,在Rails 4中,/ script/rails文件已被移至/ bin/rails。

+0

这正是我正在寻找的 - 谢谢! –

1

无法在全局范围内进行更改,因此您必须使用-b

rails s -b <ip address>

+1

关于Rails 2:'脚本/服务器-b ' –

5

使用--binding=ip参数:

rails s --binding=127.0.0.1 

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

+1

我知道这个选项 - 我希望能够在全局范围内进行更改,所以我不必每次启动时都指定它服务器。 –

+0

您可以在您的初始化程序中在您的https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb中定义'Rails :: Server :: Options#parse!'应用程序,甚至编辑您的系统中的gem文件。 – clyfe

相关问题