Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
billross00 opened this issue Aug 7, 2024 · 3 comments
Open
Labels
question Further information is requested

Comments

@billross00
Copy link

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 billross00 added the question Further information is requested label Aug 7, 2024
@peter-doggart
Copy link
Contributor

Something similar came up a few weeks back, it's worth trying the steps here?: #610 (comment)

@billross00
Copy link
Author

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

@chrisjohnson00
Copy link

chrisjohnson00 commented Oct 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants