Search code examples
pythonnginxfastapi

Can't see FastAPI documentation with 2 main.py and 1 nginx reverse proxy


I'm using fastAPI together with nginx, as a reverse proxy. I split APIs into 2 different main.py with different endpoints:

  • main.py -> main location/endopoint of APIs: / (port 5010)
  • main_slow.py -> main location/endopoint of APIs: /slow_api (port 5011)

to run them on different ports (5010,5011). This because one API is very slow and requesting APIs in series i need one to be separate from the others (in main_slow.py). Using nginx as a reverse proxy, I can call the APIs with their own endpoints under a single port (8000), then nginx will take care of passing them to the correct port of fastAPI.

All works well, the only problem is that i can't see all API documentations in /docs, but only endpoints of the first main.py (location /).

in /nginx/conf.d/ i have py_api.conf and i have configured it like this:

server {

        listen 8000;

        location / {

                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_http_version 1.1;
                proxy_set_header Connection "";

                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Origin,X-Auth-Token';
                add_header 'Access-Control-Allow-Credentials' 'true';

                if ($request_method = OPTIONS ) {
                        return 200;
                }

                proxy_pass http://localhost:5010;
                proxy_set_header Connection "Keep-Alive";
                proxy_set_header Proxy-Connection "Keep-Alive";

                auth_basic "Restricted";                                #For Basic Auth
                auth_basic_user_file /etc/nginx/.htpasswd-pyapi;  #For Basic Auth
        }
        location /slow_api{

                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_http_version 1.1;
                proxy_set_header Connection "";

                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Origin,X-Auth-Token';
                add_header 'Access-Control-Allow-Credentials' 'true';

                if ($request_method = OPTIONS ) {
                        return 200;
                }

                proxy_pass http://localhost:5011;
                proxy_set_header Connection "Keep-Alive";
                proxy_set_header Proxy-Connection "Keep-Alive";

                auth_basic "Restricted";                                #For Basic Auth
                auth_basic_user_file /etc/nginx/.htpasswd-pyapi;  #For Basic Auth
        }
}

Did I do something wrong to be able to see the documentation of both locations? Or do I have to do something about python and fastAPI?

SOLVED

To see all two documentation (for now in two different path) i added in my fastAPI project main_slow.py

app = FastAPI(  title='slow API',
                docs_url='/slow_api/docs', 
                redoc_url='/slow_api/redoc',
                openapi_url='/slow_api/openapi.json')

instead only

app = FastAPI()

Solution

  • You may want to check out these:

    Do you want to see documentations of the both API at the same route? Then have a look at Behind a Proxy: Additional Servers

    If you are okay with setting different routes for each API documentation, then you can pass the docs_url, redoc_url and openapi_url arguments to FastAPI class and handle the situation I guess. Have a look at this.