You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法访问Google Cloud虚拟机上的Fastify应用,请求排查协助

无法访问GCP虚拟机上的Fastify应用问题排查

我多次尝试后仍无法解决问题,特此求助:无法访问运行在Google Cloud虚拟机上、监听3000端口的简易"hello world"Fastify应用。向http://34.165.64.213:3000/发送GET请求时,收到如下错误:

GET http://34.165.64.213:3000/
Error: connect ECONNREFUSED 34.165.64.213:3000

应用代码

const fastify = require('fastify')({
 logger: true
})

fastify.get('/', async (request, reply) => {
 return { hello: 'fastify!' }
})

const start = async () => {
 try {
   await fastify.listen({ port: 3000 })
 } catch (err) {
   fastify.log.error(err)
   process.exit(1)
 }
}
start()

防火墙配置

{
  "allowed": [
    {
      "IPProtocol": "tcp",
      "ports": [
        "3000"
      ]
    }
  ],
  "creationTimestamp": "2023-07-08T01:09:47.176-07:00",
  "description": "",
  "direction": "INGRESS",
  "disabled": false,
  "enableLogging": true,
  "id": "5695192685623944996",
  "kind": "compute#firewall",
  "logConfig": {
    "metadata": "INCLUDE_ALL_METADATA",
    "enable": true
  },
  "name": "allow-http-3000",
  "network": "projects/videocode/global/networks/default",
  "priority": 1000,
  "selfLink": "projects/videocode/global/firewalls/allow-http-3000",
  "sourceRanges": [
    "0.0.0.0/0"
  ],
  "targetTags": [
    "allow-http-3000"
  ]
}

同一虚拟机实例的3000端口运行Express应用时可正常访问,请问这是Fastify的特定问题还是通用连接问题?


问题解答

这是Fastify的特定配置问题,而非通用连接问题。

Express默认监听0.0.0.0(允许所有网卡的请求),但Fastify默认仅监听127.0.0.1(仅接受虚拟机内部的本地请求)。因此外部请求无法连接到Fastify服务,才会出现ECONNREFUSED错误。

解决方法很简单,修改fastify.listen的配置,添加host: '0.0.0.0',让服务监听所有网卡:

const start = async () => {
 try {
   await fastify.listen({ port: 3000, host: '0.0.0.0' })
 } catch (err) {
   fastify.log.error(err)
   process.exit(1)
 }
}

修改后重启Fastify服务,外部就能正常访问了。

内容的提问来源于stack exchange,提问作者Toto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 19:32:48