random 502 bad gateway errors with Strapi endpoints - app deployed on Fly.io + postgres database on Supabase

I’ve managed deploying my Strapi app on Fly.io, connected to a PostgreSQL database on Supabase.

I’ve followed this Strapi guide, but it seems a little bit outdated so I made some custom edits on my setup.

It… sometimes work, but I regulary get 502 bad gateway errors (after one minute).

It feels like the app was idle. After a while, I start to get responses (a few seconds) and it works. A few hours later, it does not response again.

About my setup

The default port in Strapi is 1337.

But Fly.io now charges 2$/mo to use custom ports.
Since I’m a hobby user, I then need to use the “free” ports, which are 80 and 443.


module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: env('DATABASE_HOST','localhost'),
      port: env.int('DATABASE_PORT', 5432),
      database: env('DATABASE_NAME'),
      user: env('DATABASE_USERNAME','postgres'),
      password: env('DATABASE_PASSWORD'),
      schema: env('DATABASE_SCHEMA', 'public'), // Not required
      ssl: {
        rejectUnauthorized: env.bool('DATABASE_SSL_SELF', false),
    debug: false,

also tested with

  ssl: false


  http_checks = []
  internal_port = 80
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  force_https = true
  handlers = ["http"]
  port = 80

  handlers = ["tls", "http"]
  port = 443

  grace_period = "1s"
  interval = "15s"
  restart_limit = 0
  timeout = "2s"



# Expose the Strapi port

It seems that some other users have this problem on Fly.io + Supabase.
Someone says here

that would mean the connection gets closed before response headers
are fully sent back from your app.

Maybe this is related to this comment, but I don’t know how to do that:

Should you encounter a “bad gateway” error, your system may be denying NGINX due to SELinux rules. You can verify this by checking the NGINX logs at /var/log/nginx/error.log and looking for “Permission denied”.

According to Stack Overflow, the issue can typically be resolved with the following command. This allows NGINX to make network connection on your system:

sudo setsebool -P httpd_can_network_connect 1

Can anyone help ? Thanks !

What happens when you add the pool field like this?

module.exports = ({ env }) => ({
  connection: {
    // ...
    pool: {
      min: 0,
      max: 20,
1 Like

@smorimoto what happens is… that it seems to work, so far !!! Holy s* ! How did you know that, can you explain how you guessed and what’s the purpose of those settings ?
Thank you so much!

More info on the pool config object here.

Thanks Brian, i’ve checked, but it still is confusing to me - i don’t understand what this does and when I should use those parameters :slight_smile:

It’s not clear form the HTTP error what causes it; it could be anything in the loop which cause a delay, DB’s being inaccessible being one possibility :slightly_smiling_face:

My guess was that there was a problem with either the pool library or the database or the network path between them and the app was getting random 502 errors.
Further down the link provided by Brian, it appears that the Knex documentation recommends changing the default minimum pool size.
So I gave you that pointer and I’m glad the simplest solution seems to have helped you :smile:

@spiff-radio I’m guessing something similar is probably happening. I’m not very familiar with Supabase, but it’s hard to fundamentally fix problems around here from our side, and I’d recommend the option of continuing to have as few idle connections as possible.

We recently implemented a time-based pool cleanup to address this problem. However, for Strapi I believe that simply setting the minimum to zero is good enough. (We’ve actually been using Strapi recently in a similar situation with everything on Fly.)