flask-restx icon indicating copy to clipboard operation
flask-restx copied to clipboard

Recommended method to serve swagger ui behind nginx non-root location

Open billross00 opened this issue 1 year ago • 3 comments

I am trying to use flask and flask-restx to create multiple rest apis using nginx In order to support multiple rest services I add locations to the nginx config I am developing on a remote server, which is also my deployment server (one server for all rest apis) Each rest api should have its own swagger doc I am using a blueprint to alter the api.doc as shown in the restx documentation I have started with just one simple rest api I find that when serving with flask and gunicorn it works as expected, both the rest endpoint and the swagger doc work But when I use nginx the endpoint works but the swagger doc is getting "the page you are looking for is not found" I have been trying to resolve this over the last few days and have found many discussions on this issue dating back many years All the way back to before flask-restful forked to flask-restx I see many posted solutions, most involve using some combination of: defining a custom_ui changing the nginx location configuration for the reverse-proxy adding additional nginx locations for swagger.json and swaggerui I have tried many of these with no success I see the github appears to have been updated with changes based on this issue. I searched the restx documentation and could not find an example for my case. Could someone either reply with the recommended solution or point me to an example for this case. I have attached my simple restapi file as a text file restapi.txt

key component versions

Flask==3.0.3 flask-restx==1.3.0 gunicorn==22.0.0 nginx version: nginx/1.14.1

nginx conf

location /restapi { proxy_pass http://restapi/; } upstream restapi { server unix:/run/gunicorn/restapi.sock; }

billross00 avatar Aug 07 '24 16:08 billross00

Something similar came up a few weeks back, it's worth trying the steps here?: https://github.com/python-restx/flask-restx/issues/610#issuecomment-2253543816

peter-doggart avatar Aug 07 '24 16:08 peter-doggart

I made some progress. I had to change some things I didn't want to touch like the root location in the nginx.conf The issue is the path it uses to look for the swaggerui info These are the js css and swagger.json files I got the ui to populate but the "try it out" doesn't work The solution I am trying will not work for multiple rest apis on the same server.

Do you know of any way to control where it looks for the swaggerui files

billross00 avatar Aug 07 '24 23:08 billross00

Same issue here...

I serve the swagger UI from /api/v1/infra/swagger and it results in:

[2024-10-08 14:00:08,234] INFO in _internal: 127.0.0.1 - - [08/Oct/2024 14:00:08] "GET /api/v1/infra/api/v1/infra/stacks/ HTTP/1.1" 200 -
[2024-10-08 14:00:57,848] INFO in _internal: 127.0.0.1 - - [08/Oct/2024 14:00:57] "GET /api/v1/infra/swagger HTTP/1.1" 200 -
[2024-10-08 14:00:57,875] INFO in _internal: 127.0.0.1 - - [08/Oct/2024 14:00:57] "GET /swaggerui/droid-sans.css HTTP/1.1" 304 -
[2024-10-08 14:00:57,875] INFO in _internal: 127.0.0.1 - - [08/Oct/2024 14:00:57] "GET /swaggerui/swagger-ui.css HTTP/1.1" 304 -
[2024-10-08 14:00:57,876] INFO in _internal: 127.0.0.1 - - [08/Oct/2024 14:00:57] "GET /swaggerui/swagger-ui-standalone-preset.js HTTP/1.1" 304 -
[2024-10-08 14:00:57,877] INFO in _internal: 127.0.0.1 - - [08/Oct/2024 14:00:57] "GET /swaggerui/swagger-ui-bundle.js HTTP/1.1" 304 -
[2024-10-08 14:00:57,953] INFO in _internal: 127.0.0.1 - - [08/Oct/2024 14:00:57] "GET /api/v1/infra/swagger.json HTTP/1.1" 200 -

It would be nice to be able to define a swagger_ui_prefix or something for it to use instead of defaulting to the root.

Like others, I run this in K8s and having the static assets at the root directory just doesn't work.

I am able to get this to work by setting my ingress to route /swaggerui to my pod, but if I ever deploy a 2nd Python API this workaround will break.

chrisjohnson00 avatar Oct 08 '24 20:10 chrisjohnson00