fastify http2 502 error

Hello, I’m using http1 server for web socket and http2 server for https server in my fastify app.
It works fine with local environment(docker)
My code looks something like this

const fastifyHttp = Fastify({
  logger: true,
});

const fastifyHttps = Fastify({
  logger: true,
  http2: true,
  ...(process.env.NODE_ENV !== "production" && process.env.NODE_ENV !== "staging"
    ? {
        https: {
          allowHTTP1: true,
          key: fs.readFileSync("./ssl/localhost-key.pem"),
          cert: fs.readFileSync("./ssl/localhost-cert.pem"),
        },
      }
    : {}),
});

...

const PORT_HTTP = Number(process.env.PORT_HTTP) || 3000;
const PORT_HTTPS = Number(process.env.PORT_HTTPS) || 3443;

await fastifyHttp.listen({ port: PORT_HTTP, host: "0.0.0.0" });
await fastifyHttps.listen({ port: PORT_HTTPS, host: "0.0.0.0" });

and this is my fly.toml

app = "threader-be-staging"
primary_region = "nrt"

[build]
  dockerfile = "Dockerfile"

[[services]]
  internal_port = 3443
  protocol = "tcp"
  force_https = true
  processes = ["app"]
  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443
    tls_options = { "alpn" = ["h2", "http/1.1"], "versions" = ["TLSv1.2", "TLSv1.3"] }
  [[services.tcp_checks]]
    interval = "10s"
    timeout = "2s"

[[services]]
  internal_port = 3000
  protocol = "tcp"
  force_https = false
  processes = ["app"]
  [[services.ports]]
    handlers = ["http"]
    port = 80
  [[services.tcp_checks]]
    interval = "10s"
    timeout = "2s"

[[vm]]
  memory = "512mb"
  cpu_kind = "shared"
  cpus = 1

What I’m getting is 502 error for any request.
And my macine log says
[PU02] could not complete HTTP request to instance: invalid HTTP version parsed

I think it can be fixed with right toml file configuration. What am I missing here?

Hi,

I haven’t seen that particular error before :thinking:

Perhaps there’s a good reason and you need to, but it may be simper to let Fly handle the TLS termination at their proxy: TLS termination by Fly Proxy · Fly Docs That way you only need to listen for http and not have that added complexity. Let them deal with HTTP versions and things.

If you do need to, next I’d check what config is being applied by seeing what process.env is actually being used. Perhaps you have set environment variables elsewhere (like via secrets) but if not those may not be the values you expect and so it may default to using your local cert. That could cause errors.

Guesses, I’m afraid.

1 Like

Thank you Greg!
I followed the documentation you linked and it’s now perfectly working!!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.