【问题标题】:How can I deploy a Node HTTP/2 server to Google App Engine?如何将 Node HTTP/2 服务器部署到 Google App Engine?
【发布时间】:2020-11-29 07:19:00
【问题描述】:

我在 App Engine 上有一个常规的 Node(v12) http 服务器。我用http2.createSecureServer 切换到http2。它在开发中工作,但部署到 App Engine 并在服务器成功启动后响应带有 502 bad gateway 的请求...

我尝试切换到http2.createServer 以不使用 https,并且请求从未收到响应(永远加载)。该请求在 App Engine 日志资源管理器中的最后一次日志显示:

[error] 27#27: *2 upstream sent no valid HTTP/1.0 header 
while reading response header from upstream, client: 
169.254.1.1, server: _, request: "GET / HTTP/1.1", 
upstream: "http://127.0.0.1:8081/"

它似乎以某种方式期待 http1,但我不知道为什么。我也不知道为什么在8081端口,我把端口设置为8080。

花了一天的时间在 Google 和他们的文档中搜索有关将 http2 与 App Engine 结合使用的任何内容后,我已经筋疲力尽了。而且,他们的支持页面上写着“在 Stack Overflow 上发帖”..

main.js

const http2 = require('http2');
const fs = require('fs');
const app = new (require('koa'))();
const logger = require('koa-logger');
const bodyParser = require('koa-bodyparser');
const json = require('koa-json');
const cors = require('@koa/cors');
const router = require('./router.js');
const { PORT, HOST, KEY, CERT } = require('./config.js');

app.use(logger());
app.use(bodyParser());
app.use(json());
app.use(cors({ exposeHeaders: 'authorization' }));

app.use(router.routes());
app.use(router.allowedMethods());

const server = http2
  .createSecureServer(
    {
      key: fs.readFileSync(KEY),
      cert: fs.readFileSync(CERT),
      allowHTTP1: true
    },
    app.callback()
  )
  .listen(PORT, () => {
    console.log(`Koa HTTP/2 running at https://${HOST}:${PORT}`);
  });

app.yaml

runtime: nodejs12
service: api
handlers:
  - url: /.*
    script: auto
    secure: always
    redirect_http_response_code: 301

vpc_access_connector:
  name:

env_variables:
  NODE_ENV: production
  PORT:8080
  KEY: key.pem
  CERT: cert.pem

  ...

【问题讨论】:

  • 发布您的 app.yaml 和源代码。
  • @JohnHanley 我添加了相关源码
  • SSL (TLS) 由 Google 前端 (GFE) 为您处理。 GFE 和您的应用程序之间的通信是端口 8080 上的 HTTP。一旦您更正代码以仅使用 HTTP,请更新您的问题。建议。测试时不要使用重定向 301。
  • HTTP/2 需要 SSL,不是吗?因此,如果 GFE 和 Node 服务器之间的通信是 HTTP,那么 HTTP/2 将被转换,我不会从使用它中受益。对吗?
  • 那么您就不能使用 App Engine。如果您想控制 TCP/IP 堆栈,则需要使用 Compute Engine。

标签: node.js google-app-engine


【解决方案1】:

如果您想同时使用 HTTP/2 和 App Engine,您可以考虑将Load Balancing 与您的无服务器应用程序一起使用。 Google App Engine 中的502 错误可能涉及多种不同的可能性。

错误消息 BAD_GATEWAY

消息中带有 BAD_GATEWAY 的错误代码 502 通常表示 App Engine 因内存不足而终止了应用程序。默认的 App Engine 灵活虚拟机只有 1GB 内存,只有 600MB 可用于应用程序容器。

错误代码 502 或 503

App Engine 可能需要几分钟才能成功响应请求。如果您发送请求并返回 HTTP 502、503 或其他一些服务器错误,请稍等片刻,然后重试该请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 2021-04-11
    相关资源
    最近更新 更多