can't see fastAPI documentation with 2 main.py and 1 nginx reverse proxy

Viewed 1066

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()
2 Answers

You have to change the order of location blocks. Nginx terminates location matching at the first successful match. In your case, all requests to /slow_api are matched to the first location block and the second block is never checked. You can check the official documentation for more details.

Docs

Related