2011-12-12 70 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

相關問題