Ruby-on-rails 从本地网络上的另一台计算机访问 webrick/rails
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7325663/
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
Access webrick/rails from another computer on local network
提问by agmcleod
I have a rails application running on localhost:3000. I wish to access it from another computer on the same network. I feel like i've done this before with ease, but it's giving me some grief. I can ping the IP of the computer just fine, but hitting ip:3000 in the browser doesnt work. I tried launching rails s -b ipaddressas well, and no luck.
我有一个在 localhost:3000 上运行的 rails 应用程序。我希望从同一网络上的另一台计算机访问它。我觉得我以前很轻松地做到了这一点,但这让我有些悲伤。我可以ping通电脑的IP就好了,但是在浏览器中点击ip:3000不起作用。我也尝试启动rails s -b ipaddress,但没有运气。
Suggestions?
建议?
采纳答案by VNO
Try running the server on port 80 instead, your firewall is probably blocking port 3000.
尝试在端口 80 上运行服务器,您的防火墙可能阻止了端口 3000。
回答by OneHoopyFrood
After making sure your server side firewall is open to the incoming connection on high ports (this is normally true and the default port is 3000, so you probably don't have to do anything) you must also start the server like this:
在确保您的服务器端防火墙对高端口上的传入连接开放(这通常是正确的,默认端口是 3000,所以您可能不必做任何事情),您还必须像这样启动服务器:
rails server -b 0.0.0.0
which binds it to the universal address. It binds to localhostby default.
它将它绑定到通用地址。它localhost默认绑定到。
Using this method you don't have to bind to port 80, but you can like this:
使用此方法您不必绑定到端口 80,但您可以这样:
rails server -b 0.0.0.0 -p 80
(If you're using rvm then you may need to use rvmsudo)
(如果您使用的是 rvm,那么您可能需要使用rvmsudo)
To make this change more permanent edit your config/boot.rband add this:
要使此更改更持久,请编辑config/boot.rb并添加以下内容:
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge(Host: '0.0.0.0', Port: 3000)
end
end
end
Then you should only have to use rails s
那么你应该只需要使用 rails s
回答by tomascharad
rails server -b 0.0.0.0 -p 8000
This worked for me. No firewall issues, and no need to give super user permissions.
这对我有用。没有防火墙问题,也不需要授予超级用户权限。
回答by Beauty
Yes, this was a good answer in general:
rails server -b 0.0.0.0If you use Ubuntu, you probably have to open the port in the firewall:
sudo ufw allow 3000If your system is running in VirtualBox, you have to check your Network Settings.
In the case of network mode
NATyou have to click to the extended options and there to Port Forwarding. Add a rule for TCP protocoll, host port 3000 (or any other), and guest port 3000.
是的,这是一个很好的答案一般:
rails server -b 0.0.0.0如果您使用Ubuntu,您可能需要在防火墙中打开端口:
sudo ufw allow 3000如果您的系统在VirtualBox 中运行,您必须检查您的网络设置。
在网络模式的情况下,
NAT您必须单击扩展选项,然后单击端口转发。为 TCP 协议、主机端口 3000(或任何其他)和访客端口 3000 添加规则。
回答by Lester Peabody
Assuming Webrick starts without issue, this is 100% a firewall issue. You should provide some specifications, like what operating system your host is running and whether or not you have administrator privileges as far as controlling the firewall.
假设 Webrick 启动没有问题,这 100% 是防火墙问题。您应该提供一些规范,例如您的主机运行的操作系统以及您是否具有控制防火墙的管理员权限。
If you're on Linux and running the iptables firewall service, you need to add a rule to accept traffic over port 3000. It would look something like:
如果你在 Linux 上运行 iptables 防火墙服务,你需要添加一个规则来接受端口 3000 上的流量。它看起来像:
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
That command would be a one-time-only solution though, you'd need to extend your current iptables rules script to make it permanent every time your system boots or logs in.
但是,该命令将是一次性解决方案,您需要扩展当前的 iptables 规则脚本,以使其在每次系统启动或登录时永久生效。
If you're running Windows, depending on whether you're running XP or Vista/7, you'd need to do something similar. I'm going to assume you're in the Vista/7 environment, and you would just need to follow the steps provided through this guide http://windows.microsoft.com/en-US/windows7/Open-a-port-in-Windows-Firewall.
如果您运行的是 Windows,则取决于您运行的是 XP 还是 Vista/7,您需要执行类似的操作。我将假设您在 Vista/7 环境中,您只需要按照本指南http://windows.microsoft.com/en-US/windows7/Open-a-port提供的步骤进行操作-in-Windows-防火墙。
回答by lsc
One reason is your ip is not bind to the rails server. You can bind the ip with -b command option.
原因之一是您的 ip 未绑定到 rails 服务器。您可以使用 -b 命令选项绑定 ip。
Usage: rails server [mongrel, thin etc] [options]
-p, --port=port Runs Rails on the specified port.
Default: 3000
-b, --binding=IP Binds Rails to the specified IP.
Default: localhost
回答by Mosab.Mohamed
I am using foreman to manage my Procfile-based application.
我正在使用 foreman 来管理我的基于 Procfile 的应用程序。
Adding -b 0.0.0.0to my bundle exec rails scommand in Procfile worked for me.
在 Procfile 中添加-b 0.0.0.0到我的bundle exec rails s命令对我有用。

