node.js - 如何移植 Google Compute Engine 实例?

标签 node.js mongodb port google-compute-engine

我已经使用 Google Compute Engine 平台设置了 VPS。在该实例中,我建立了一个在默认端口 21017 本地托管的 MongoDB 数据库。我还设置了一个基于 REST API 的 NodeJS 服务器,并快速监听端口 8080 上的连接。

现在,我只能在内部访问 NodeJS 站点。如何将 VPS 端口 8080 暴露给外部 IP 地址,以便我可以在任何地方访问 API?

我尝试按照这篇文章的答案进行操作:Enable Access Google Compute Engine Instance Via HTTP Port .

但这并没有解决我的问题

最佳答案

Default Firewall rules

默认情况下,Google Compute Engine 防火墙会阻止虚拟机的所有入口流量(即传入网络流量)。如果您的虚拟机是在默认网络上创建的,则允许使用 22 (ssh)、3389 (RDP) 等少数端口。

default firewall rules are documented here .

打开入口端口

ingress firewall rules are described here .

建议的方法是创建一条防火墙规则,允许端口 8080 访问包含您选择的特定标记的虚拟机。然后将此标签关联到您想要允许入口 8080 的虚拟机上。

如果您使用 gcloud,则可以使用以下步骤执行此操作:

# Create a new firewall rule that allows INGRESS tcp:8080 with VMs containing tag 'allow-tcp-8080'
gcloud compute firewall-rules create rule-allow-tcp-8080 --source-ranges 0.0.0.0/0 --target-tags allow-tcp-8080 --allow tcp:8080

# Add the 'allow-tcp-8080' tag to a VM named VM_NAME
gcloud compute instances add-tags VM_NAME --tags allow-tcp-8080

# If you want to list all the GCE firewall rules
gcloud compute firewall-rules list

这里是another stack overflow answer其中将引导您了解如何使用 Cloud Console Web UI(以及 gcloud)允许特定端口上的传入流量进入虚拟机。

Static IP addresses

您链接的答案仅描述了如何分配静态 IP 地址并将其分配给您的虚拟机。此步骤独立于防火墙规则,因此如果您想使用静态 IP 地址,可以组合使用。

关于node.js - 如何移植 Google Compute Engine 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45136282/

相关文章:

node.js - Firebase - Node 云函数解析触发器时出错 : Cannot find module 'firebase-functions'

javascript - 如何在node.js中使用Promise.all和Request?

javascript - 如何使用 Node.js 创建自引用 MongoDB 模式并发布路由?

postgresql - 中央操作系统 6 : Not Allowing Connection to Port 5432

python - Django 在每个端口分别为每个应用程序提供服务

c++ - 获取用于 TCP 连接的随机端口

mysql - 如何在 NodeJS 中的相同代码中返回 promise 并关闭 mysql 连接

arrays - 我如何在 Mongoose 中查询以获得 "answers"数组,其中 'objective'数组内的 'answers'是 "Awareness"

mongodb - 如何使用 MONGOLAB_URI 通过 mongo shell 连接到数据库?

mongodb - Mongoose 更新 $setOnInsert 更改 updatedAt 时间戳,即使没有更新发生