连接到我的golang服务器从外部世界。

huangapple go评论80阅读模式
英文:

Connecting to my own golang sever from outside world

问题

我确实有这段用go编写的代码:

// firstserver - 我的第一个服务器
package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", Handler)
	err := http.ListenAndServe(":80", nil)
	if err != nil {
		log.Fatal(err)
	}
}

// Handler - 一个处理普通请求的处理函数
func Handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "你已连接")
}

现在,当我在浏览器中输入localhost:80(或者只输入localhost)时,页面加载完成,我看到了消息你已连接。现在我想尝试从其他设备连接。所以我拿起了我的另一台笔记本电脑。我检查了我的适配器地址,它是192.168.0.15。所以当我在另一台笔记本电脑上输入192.168.0.15:80时,它又一次工作了,很酷!

我主要是想做一个简单的服务器,以便在上大学时可以在手机上使用它,查看课程表,记录一些笔记等等。我拿起了我的智能手机,断开了WiFi连接,切换到了手机的移动网络,然后愚蠢地再次输入了192.168.0.15:80。我知道它不会工作,因为它不是我的外部IP地址。果然没有工作。

所以我查了一下我的ISP提供的IP地址,它看起来像这样:88.156.xxx.xx。现在我在手机上输入88.156.xxx.xx:80,但它不起作用。我尝试在SO和其他网络资源上搜索如何从局域网外部远程连接到我的服务器,但没有找到答案。有没有办法在不支付费用或类似的事情的情况下实现这一点?

附注:我住在一个租来的房间里,房子里不只有我一个人,所以我没有办法配置路由器(无论是手动还是通过网络配置)。

英文:

So I do have this code written in go:

// firstserver - my first server
package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", Handler)
	err := http.ListenAndServe(":80", nil)
	if err != nil {
		log.Fatal(err)
	}
}

// Handler - a handler function for a normal enter
func Handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "You're connected")
}

Now, when I go to my browser and type localhost:80 (or just localhost), the page loads, and I see the message You're connected. Now I wanted to try to connect from other devices too. So I took my other laptop. I checked my adapter address, its 192.168.0.15. So when I typed 192.168.0.15:80 on the other laptop, it worked again, cool!

Primarly I am trying to do some simple server to use it on my phone when I am at college, check the schedules, have some notes, etc. I took my smartphone, I disconnected from my WiFi, switched to my smartphones internet and dumbly typed 192.168.0.15:80 again. I knew it wouldn't work, cuz it's not my external IP. And it didn't.

So I looked up the IP address that my ISP provider gives me, it looks like this: 88.156.xxx.xx. Now on my phone I typed 88.156.xxx.xx:80 and it didn't work. I tried to search SO and other resources on the web on how to connect remotely to my server from outside of LAN. And I didn't find an answer. Is there a way to do that without, I don't know, paying for sorts of things, or something like this?

P.S I'm living in a rented room, there's not only me in the house, so I don't have a way to configure a router (manually or over the web)

答案1

得分: 1

你可以通过在具有公共IP的AWS免费层实例上部署它来实现。一旦你完成EC2实例的启动,将你的Web服务器的二进制文件复制到该实例上,使用以下命令:scp -i mykey.pem somefile.txt root@my.ec2.id.amazonaws.com:/

你需要确保该实例的安全组允许来自互联网(0.0.0.0/0)在该端口上的入站流量。

要访问它:
使用EC2实例的公共IP或公共DNS,以及你的服务器的端口号(在你的情况下是80)。

英文:

One way you can do it is deploy it on an AWS Free Tier instance with public IP(https://aws.amazon.com/free/).
Once you are done spinning up the EC2 instance (https://aws.amazon.com/free/), copy the binary of your web server to that instance scp -i mykey.pem somefile.txt root@my.ec2.id.amazonaws.com:/

You need to ensure that the security group on that instance allows ingress from the internet(0.0.0.0/0) on that port

To access it:
Use either the public IP or public DNS of the EC2 instance along with the port number of your server(in your case 80)

huangapple
  • 本文由 发表于 2017年9月12日 11:08:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/46167279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定