【问题标题】:How to communicate with the nodeJS server setup in the Google cloud virtual machines(Windows server) through internet?如何通过互联网与谷歌云虚拟机(Windows 服务器)中设置的 nodeJS 服务器进行通信?
【发布时间】:2021-12-06 03:33:38
【问题描述】:

我正在使用 soocket.io 在 android 中开发一个聊天应用程序。此应用程序需要通过 Internet 与我的 Google 云虚拟机中在端口 4000 上侦听的 nodejs 应用程序之一进行通信。 GCP 提供内部和外部 IP。当我在 VM 的浏览器中运行 internalIP:4000 时,我可以看到它运行良好。但是在虚拟机之外,当我运行ExternalIP:4000 时,它会出错。我不知道为什么它不起作用。

这是防火墙配置

【问题讨论】:

    标签: node.js google-cloud-platform socket.io


    【解决方案1】:

    所以您已经确定您的服务运行良好并且在正确的端口 (4000) 上。

    现在你必须暴露在互联网上。默认情况下 this port is blocked for any traffic 通过 GCP's Firewall。这样,只有 SSH、RDP 或 ICMP 协议的端口才可以连接到您的虚拟机。如果您需要非标准端口,则必须创建适当的防火墙规则。

    您可以通过使用 Cloud Shell 运行或在 VM 的命令提示符中运行它来创建它:

    gcloud compute --project=my-project-name firewall-rules create my-app-port \
    --direction=INGRESS --priority=1000 --network=default \
    --action=ALLOW --rules=tcp:4000 --source-ranges=0.0.0.0/0 \
    --target-tags=my-app-server
    

    或使用Cloud Console 创建它。如果您有任何疑问,请查看documentation

    在我的示例中,我使用了my-app-server 网络标签。您需要运行应用服务器的add it to your VM - 否则该规则将不起作用。最好这样做,因为您只在单个 VM 上公开一个额外的端口,这是最安全的解决方案。

    【讨论】:

    • @wojtek_B 感谢您的回答。我会试试的。
    • @wojtek_B 我将防火墙规则配置为允许在 tcp:4000 进行连接。但是当我从外部 IP 运行时,它仍然在浏览器中显示“站点无法访问”消息。
    • 有问题的虚拟机是否可以访问互联网?你能从外面ping你的VM吗?请使用gcloud compute firewall-rules describe firewall_rule_name 的输出更新您的问题。
    • @Wojtek_B 我可以从外部 ping 虚拟机。我更新了我的问题,没有“gcloud compute firewall-rules describe firewall_rule_name”,请查找。
    • 你使用Identity Aware Proxy 吗?它可能会阻止您的连接。尝试从同一 VPC 网络中的不同 VM 访问您的应用程序 - 是否有效?从其他 VM 或 Internet 尝试:curl VM'sIP:4000 并将结果粘贴到您的原始问题中。
    猜你喜欢
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2016-08-20
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2017-08-28
    相关资源
    最近更新 更多