Ruby-on-rails Rails 4.2开发服务器如何修改默认绑定ip?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28668436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the default binding ip of Rails 4.2 development server?
提问by Huang Tao
After upgrading our team's rails application to 4.2, as the release notementioned, the default ip rails serverbinds to is changed to localhostfrom 0.0.0.0.
将我们团队的rails应用升级到4.2后,正如发布说明中提到的,默认iprails server绑定到更改为localhostfrom 0.0.0.0。
We develop with Vagrant, and want the development server to be accessible directly from browser on the host machine.
我们使用 Vagrant 进行开发,并希望可以直接从主机上的浏览器访问开发服务器。
Instead of typing rails s -b 0.0.0.0every time from now on, I wonder if there's any more elegant solution, so that we can still use sth as simple as rails sto start the server. Perhaps:
以后不用rails s -b 0.0.0.0每次都打字了,不知道有没有更优雅的方案,让我们依然可以像rails s启动服务器一样简单。也许:
- a config file
rails sreads where I can modify the default binding ip (without using-c) - port forward with vagrant (tried but failed, see problem encountered below)
- a monkey patch to rack, that changes the default binding ip
- 一个配置文件
rails s读取我可以修改默认绑定 ip 的位置(不使用-c) - 使用 vagrant 转发端口(尝试但失败,请参阅下面遇到的问题)
- 一个猴子补丁到机架,它改变了默认的绑定 ip
The real goal behind this is that I want the upgrade to be smooth among our team, avoiding the glitch that people will have to constantly restarting their rails server due to the missing -b 0.0.0.0part.
这背后的真正目标是我希望我们的团队能够顺利进行升级,避免由于缺少-b 0.0.0.0部分而导致人们不得不不断重新启动他们的 rails 服务器的故障。
I tried vagrant port forwarding, but still get Connection Refusedwhen I visit localhost:3000on the host machine. The two configuration lines I tried was:
我试过 vagrant 端口转发,但是Connection Refused当我localhost:3000在主机上访问时仍然得到。我尝试的两条配置行是:
config.vm.network "forwarded_port", guest: 3000, host: 3000
config.vm.network "forwarded_port", guest: 3000, guest_ip: '127.0.0.1', host: 3000
Didn't find any relevant instructions in the official docs. Any help will be appreciated.
在官方文档中没有找到任何相关说明。任何帮助将不胜感激。
采纳答案by imarcelolz
I'm having the same issue here and I found today a better solution. Just append this code to your config/boot.rb and it should work with vagrant.
我在这里遇到了同样的问题,我今天找到了一个更好的解决方案。只需将此代码附加到您的 config/boot.rb 中,它就可以与 vagrant 一起使用。
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge(Host: '0.0.0.0', Port: 3000)
end
end
end
ps: Its based on: this answer
ps:它基于:这个答案
回答by zwippie
You can use foremanto run a Procfilewith your custom commands:
您可以使用foreman来运行Procfile您的自定义命令:
# Procfile in Rails application root
web: bundle exec rails s -b 0.0.0.0
Now start your Rails application with:
现在启动你的 Rails 应用程序:
foreman start
The good thing about foreman is that you can add other applications to the Procfile (like sidekiq, mailcatcher).
foreman 的好处是您可以将其他应用程序添加到 Procfile(如 sidekiq、mailcatcher)。
The bad thing about foreman is that you have to train your team to run foreman startinstead of rails s.
foreman 的坏处是你必须训练你的团队跑foreman start而不是rails s。
回答by Bian Jiaping
Met the same problem. Found the blog Make Rails 4.2 server listens to all interfaces.
遇到同样的问题。发现博客Make Rails 4.2 server listens to all interfaces。
Add the following to config/boot.rb
将以下内容添加到 config/boot.rb
require 'rails/commands/server'
module Rails
class Server
alias :default_options_bk :default_options
def default_options
default_options_bk.merge!(Host: '0.0.0.0')
end
end
end
回答by phlegx
If you put the default options on config/boot.rbthen all command attributes for rake and rails fails (example: rake -Tor rails g model user)!So, append this to bin/railsafter line require_relative '../config/boot'and the code is executed only for the rails server command:
如果您设置默认选项,config/boot.rb则 rake 和 rails 的所有命令属性都会失败(例如:rake -Tor rails g model user)!因此,将其附加到bin/rails行之后require_relative '../config/boot',代码仅针对 rails 服务器命令执行:
if ARGV.first == 's' || ARGV.first == 'server'
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge(Host: '0.0.0.0', Port: 3000)
end
end
end
end
The bin/railsfile loks like this:
该bin/rails文件看起来像这样:
#!/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: '0.0.0.0', Port: 3000)
end
end
end
end
require 'rails/commands'
回答by jsmartt
For Rails 5.1.7 with Puma 3.12.1 the selected answer does not work, but I accomplished it by adding the following to my config/puma.rbfile:
对于带有 Puma 3.12.1 的 Rails 5.1.7,所选答案不起作用,但我通过将以下内容添加到我的config/puma.rb文件中来完成它:
set_default_host '0.0.0.0' # Note: Must come BEFORE defining the port
port ENV.fetch('PORT') { 3000 }
I determined this by inspecting the dsl file. It uses instance_evalon that file, so there are probably other ways to do it, but this seemed the most reasonable to me.
我通过检查dsl 文件确定了这一点。它用于instance_eval该文件,因此可能还有其他方法可以做到这一点,但这对我来说似乎是最合理的。
回答by John Bachir
Here's a simpler solution that I'm using. I already like/need dotenvand puma-heroku, so if using those doesn't work for you then this might not be for you.
这是我正在使用的一个更简单的解决方案。我已经喜欢/需要dotenv和puma-heroku,所以如果使用它们对您不起作用,那么这可能不适合您。
/config/puma.rb
/配置/美洲狮.rb
plugin :heroku
Gemfile
文件
gem 'dotenv-rails', groups: [:development, :test]
.env
.env
PORT=8080
Now I can start both dev and production with rails s.
现在我可以使用rails s.
回答by prusswan
Switch to Pumaand specify portin config/puma.rb, e.g.:
切换到Puma并port在 中指定config/puma.rb,例如:
port ENV.fetch("PORT") { 3000 }
Apparently it will bind to 0.0.0.0 for the specified port: https://github.com/puma/puma/issues/896
显然它会绑定到指定端口的0.0.0.0:https://github.com/puma/puma/issues/896
回答by Paulo Fidalgo
If you use docker or another tool to manage the environment variables, you can set the HOSTenvironment variable to the IP you need to bind.
如果使用docker或者其他工具管理环境变量,可以将HOST环境变量设置为需要绑定的IP。
Example:
HOST=0.0.0.0
例子:
HOST=0.0.0.0
Add it to docker.envfile if you use Docker or .envif you use foreman.
docker.env如果您使用 Docker 或.env使用工头,请将其添加到文件中。
回答by Daniel Garmoshka
For Rails 5 with Puma the selected answer does not work. You may get such error: cannot load such file -- rails/commands/server
对于带有 Puma 的 Rails 5,所选答案不起作用。你可能会收到这样的错误:cannot load such file -- rails/commands/server
For proper solution add following to config/puma.rb:
要获得正确的解决方案,请将以下内容添加到config/puma.rb:
bind 'tcp://0.0.0.0:3000'

