简体   繁体   中英

How can I deploy a Node HTTP/2 server to Google App Engine?

I have a regular Node(v12) http server on App Engine. I switched to http2 with http2.createSecureServer . It works in development, but deploying to App Engine and after the server starts successfully it responds to requests with 502 bad gateway...

I tried to switch to http2.createServer to not use https, and the request never receives a response (forever loading). The last log in App Engine Log Explorer for that request shows:

[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/"

It seems like it somehow is expecting http1, but I don't know why. I also don't know why it's on port 8081, I have the port set to 8080.

After spending my day searching Google and their docs for anything on using http2 with App Engine, I'm burnt out. And, their support page says "post on 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

  ...

If you want to use HTTP/2 and App Engine together you might consider using Load Balancing with your serverless applications. A 502 Error in Google App Engine might refer to several different possibilities.

Error message BAD_GATEWAY

An error code 502 with BAD_GATEWAY in the message usually indicates that App Engine terminated the application because it ran out of memory. The default App Engine flexible VM only has 1GB of memory, with only 600MB available for the application container.

Error code 502 or 503

App Engine may take a few minutes to respond successfully to requests. If you send a request and get back an HTTP 502, 503, or some other server error, wait a minute and try the request again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM