Rack::Access 㧠IP ã¢ãã¬ã¹å¶é
Rails ã¢ããªã±ã¼ã·ã§ã³ã§ IP ã¢ãã¬ã¹å¶éãå¿ è¦ã«ãªã£ãéã«ã¯ãåºæ¬çã«ä¸ä½ã®ãªãã¼ã¹ãããã·ï¼Apache ã¨ã Nginx ã¨ãï¼ã§è¡ããã¨ãå¤ãã¨æãã¾ãããä¸æ¹ã§ Rails æ¬ä½ã§å¶éããããªããã°ãªããªããã¨ãããã¾ãï¼Heroku ã ã¨ãï¼
ã§ãããã Rails ã® controller 層ã¨ãã§å®è£ ããã®ã¯ã¡ãã£ã¨å«ã§ããããç¹ã«ãã¸ãã¹ãã¸ãã¯ã§ã¯ãªããã¨ãå¤ãã§ããã
ã¨ããããã§ãIP ã¢ãã¬ã¹å¶éã Rack Middleware 層ã§å®è£ ãã Rack::Access ã¨ããã¢ã¸ã¥ã¼ã«ã使ã£ã¦ã¿ã¾ããããã㯠GitHub - rack/rack-contrib: Contributed Rack Middleware and Utilities ã«å«ã¾ãã¦ãã¾ãã使ãæ¹ã¯ Gemfile ã«æ¸ã㦠bundle ã㦠config/application.rb ã«è¨å®ãæ¸ãã¦å®äºã§ãã
Gemfile:
gem 'rack-contrib', :require => 'rack/contrib'
config/application.rb
config.middleware.use "Rack::Access", '/' => [ '127.0.0.1', '192.168.1.0/24' ]
ãã㧠127.0.0.1 ããã㯠192.168.1.0/24 以å¤ããã®ã¢ã¯ã»ã¹ã«ã¯ Forbidden ãè¿ãããã«ãªãã¾ãã
å½ç¶ãå®è¡ç°å¢ã«ãã£ã¦ç°ãªãå¶éãããããã¨ã«ãªãã¨æãã¾ãããããã environments/ 以ä¸ã«æ¸ãã¦ãããã¨æãã®ã§ãããé¢åãªã®ã§èªå㯠YAML ã«æ¸ãã¾ããã
config/application.rb
config.middleware.use "Rack::Access", YAML.load(open(Rails.root + "config/access.yml", &:read))[Rails.env]
config/access.yml
localnet: &localnet /: - 127.0.0.1 - 192.168.1.0/24 development: <<: *localnet test: <<: *localnet production /: - xxx.xxx.xxx.xxx # å ¬é対象ã¢ãã¬ã¹
åçã«å¶éãå¤æ´ããã¨ãã使ãæ¹ãããããã°ããå°ãè¤éã«ããªããã°ããã¾ãããï¼ä¸å¿ã§ããã¯ãï¼ãã¤ã³ãã©åãã¢ããªãªã©ã§ã¯ãã®ç¨åº¦ã§ååã§ã¯ãªãã§ããããã
ãã¡ãã Rack middleware ãªã®ã§ã Rails 以å¤ã®ã¢ããªã±ã¼ã·ã§ã³ã§ãåæ§ã«ä½¿ããã®ãããã§ããã