Problems deploying FastAPI using gunicorn: getting constant 307 temporary redirect

I’m trying to deploy a FastAPI project using docker. I have a very simple route like this:

router = APIRouter()

@router.get("/")
def get_index():
    return {"status": "ok"}

On my fly.toml, I have this to start the web process:

[processes]
web = "gunicorn -b :8080 -w 4 -k uvicorn.workers.UvicornWorker app.main:app"

The problem is that I keep getting 307 Temporary Redirect responses no matter which path I try to request:

> curl -vvv https://<my-app>.fly.dev/

< HTTP/2 307
< location: https://<my-app>.fly.dev/

If I open the URL in the browser, it just ends up in a 307 loop without ever working.

I tried numerous config changes:

  • Using uvicorn instead of gunicorn (uvicorn app.main:app --host 0.0.0.0 --port 8080)
  • Using only http1.1 (curl -vvv --http1.1 ...)
  • Specific FastAPI code changes

It seems like it might be something related to the way Fly proxies the requests to the FastAPI, but I can’t figure out what it is.

Help appreciated! :pray:

I believe that’s the HTTPSRedirectMiddleware.

Looking at it, it’s not accounting for proxies (headers like x-forward-ssl and x-forwarded-proto)?

I would remove that. I believe by default we force an https redirect unless disabled.

Ah, of course, I suspected some proxy-related stuff, but couldn’t figure it out. Thanks!