启动 rails 应用程序,从本地网络外部向应用程序发出请求 [英] Boot up rails app, make request to app from outside local network

查看:61
本文介绍了启动 rails 应用程序,从本地网络外部向应用程序发出请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这是一个非常基本的问题,但我找不到答案.我已经看过这个问题这个问题这个问题这个问题这个问题,但没有一个能帮助我找到答案.

I'm sure this is a pretty basic question, but I could not find an answer to it. I already looked at this question, this question, this question, this question, and this question and none of them helped me reach an answer.

我正在使用 Rails 4.2.我创建了一个非常简单的应用程序来测试我是否可以执行以下操作:

I am using Rails 4.2. I created a very simple app to test out if I could do the following:

我想看看是否可以在我的计算机上启动我的 rails 应用程序,然后从本地网络的外部访问该应用程序.例如:从我的手机(未连接到本地网络)我想发出请求并成功获得响应.

I wanted to see if it was possible for me to boot up my rails app on my computer, and then access the app from outside of my local network. For example: from my phone (which is not connected to the local network) I want to make a request and successfully get a response.

我假设我首先需要获取我的外部/可路由 IP 地址.我意识到我的 ISP 为我的路由器提供的 IP 地址不是静态的,但只是为了演示目的,我想看看我是否可以成功地发出一个请求.

I assumed that I first needed to get my external/routable ip address. I realize that the IP address my ISP provides my router is not static, but just for demo purposes I wanted to see if I could just successfully make a single request.

我去谷歌输入我的 ip 是什么",它返回了我的外部 ip 地址(假设我的外部 ip 地址是:11.111.111.111).

I went to google and typed in "What is my ip" and it came back with my external ip address (let's pretend my external ip address is: 11.111.111.111).

在我的 rails 应用程序中:然后我执行了以下命令:

Within my rails app: I then did the following command:

rvmsudo rails s -p 80 -b 11.111.111.111 

我想绑定到外部IP地址,我想监听80端口.我做rvmsudo是因为,据我所知,1000以下的所有端口都需要更高的权限.

I want to bind to the external ip address, and I want to listen on port 80. I do the rvmsudo because, as I understand it, all ports below 1000 require higher privileges.

它出错并显示以下内容:

It errors out and says the following:

无法分配请求的地址 - bind(2)

Can't assign requested address - bind(2)

我试图理解为什么会出错.我不明白什么?我错过了一步吗?这是不可能的吗?是我的 ISP 使我无法绑定到该外部 IP?我需要在我的路由器上做额外的配置吗?

I am trying to understand why this is erroring out. What am I not understanding? Am I missing a step? Is it not possible to do this? Is my ISP making it so that I cannot bind to that external IP? Do I need to do additional configuration on my router?

我意识到我可以只使用 heroku.这更像是一个实验,看看这是否可能.提前致谢!

I realize that I could just use heroku. This is more of an experiment to see if this is even possible. Thanks in advance!

推荐答案

您不能将套接字绑定到您的外部 IP.该 IP 由您的 ISP 分配给您的调制解调器(或任何您用来连接互联网的设备).您只能绑定到您计算机的接口.

You can not bind the socket to your external IP. This IP is assigned to your modem (or whatever you use to connect to the internet) by your ISP. You can only bind to interfaces of your computer.

有多种解决方案:

  • 绑定到 0.0.0.0:3000(或您的计算机 IP,而不是本地主机),并确保您的网络将您的公共 IP 转发到该端口上的计算机.
  • 使用类似 https://ngrok.com/ 之类的东西
  • Bind to 0.0.0.0:3000 (or your computers IP, not localhost) and make sure that your network forwards your public IP to your computer on that port.
  • Use something like https://ngrok.com/ easy and simple

这篇关于启动 rails 应用程序,从本地网络外部向应用程序发出请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆