How can I use FastApi through IIS using httpPlatformHandler

400 views Asked by At

I am having some issues trying to get a Python 3.7 FastAPI working through IIS.

Following instructions to configure Microsoft httpPlatformHandler, I have managed to get a basic response working through adding the following configuration:

<httpPlatform requestTimeout="00:05:00" startupTimeLimit="120" startupRetryCount="3" stdoutLogEnabled="true" stdoutLogFile="c:\logs\fast" processPath="c:\python3\python.exe" arguments="-m uvicorn webapis.api.fast:app --host 127.0.0.1 --port %HTTP_PLATFORM_PORT%">
    <environmentVariables>
        <environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
    </environmentVariables>
</httpPlatform>

This works, in the sense that Uvicorn is correctly started and I do get back a response object generated by FastAPI:

{
    "detail": "Not Found"
}

The issue is that the actual query is always returning a 404 response and the logs show that information I don't want to pass is being sent to the API:

INFO:     [::1]:56395:0 - "GET /webapis/api/fast.py/hello/bob HTTP/1.1" 404 Not Found

Essentially, I need to strip /webapis/api/fast.py from what is being passed into my FastAPI. I think the Reverse proxy with URL rewrite is what I need here, but I can't get this to work. I have tried adding the following to my web.config with a separately started Uvicorn server on port 8000

<rewrite>
    <rules>
        <rule name="Reverse Proxy to FastApi" stopProcessing="true">
            <match url="^webapis/api/fast.py/(.*)" />
            <action type="Rewrite" url="http://localhost:8000/{R:1}" />
        </rule>
    </rules>
</rewrite>

This results in an IIS generated 404 error and I can see from the logs in the Uvicorn process running on port 8000 that no request is ever forwarded to it. Thinking the httpPlatformHandler config may be interfering, I tried changing the fast.py portion of the match URL to simply fast, but the same result occurs

I'm not very familiar with IIS and would really appreciate any guidance anyone could provide on how to properly configure httpPlatformHandler / URL Rewrite to achieve my goal

Thank you

0

There are 0 answers