Search code examples
iisfastapiwindows-serversubnetuvicorn

Cannot reach FastAPI server running on Windows Server 2016 from subnet


I have a Fast API web server running on Windows Server 2016 (inside a poetry env shell). And I am trying to hit it from another server but in the same subnet. It times out. But I am able to hit it locally.

Similary, I am running an apache web server python3 -m http.server 8000 and I can hit it successfully.

Why am I able to reach the simple http.server and not the FastAPI (Uvicorn) server?

The windows server 2016 is running IIS.

My FastAPI main.py file

from fastapi import FastAPI, APIRouter
from fastapi.middleware.cors import CORSMiddleware


app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get('/')
def health():

    return {
        "Status": "Success",
        "Message": "Server running"
    }

Start command - uvicorn main:app --reload --port 8000. Running this inside a poetry environment shell.


Solution

  • When using Uvicorn to start the FastAPI server, please check the Uvicorn bind address. By default, Uvicorn binds to 127.0.0.1 (localhost). Please make sure it's binding to either the server's IP address or 0.0.0.0 to allow connections from other computers.

    When starting Uvicorn, you can specify the --host parameter to bind to a specific IP address. For example:

    uvicorn main:app --reload --host 0.0.0.0 --port 8000
    

    Similar thread: How to access FastAPI backend from a different machine/IP on the same local network?