Swagger UI not working as expected while service behind Nginx reverse-proxy Swagger UI not working as expected while service behind Nginx reverse-proxy nginx nginx

Swagger UI not working as expected while service behind Nginx reverse-proxy


The problem was for the swagger-ui-express middleware that redirect user to host/api-docs and don't use the prefix of path, so I solved this problem with a trick I use middleware with this path :

const swaggerUi = require('swagger-ui-express');const swaggerDocument = require('./swagger.json');app.use('/app-prefix/api-docs',swaggerUi.serve, swaggerUi.setup(swaggerDocument));

and in nginx I defined two location :

  location /app-prefix/api-docs {      proxy_pass http://172.18.0.89:3000/app-prefix/api-docs;  }  location /app-prefix/ {      proxy_pass http://172.18.0.89:3000/;  }

so when user request to nginx , nginx route it to application second path :/app-prefix/api-docs

and after that swagger middlware redirect it to host/app-prefix/api-docsand redirect to correct path,now application route and swagger works fine.


add this options and test it :

    explorer: true,    swaggerOptions: {        validatorUrl: null    }};app.use('/api-docs',swaggerUi.serve, swaggerUi.setup(swaggerDocument, swaggerOption));```


This is an old question, but I just run into the same problem. I am able to resolve this without using ngnix rewrite.

// serve the swagger ui in a temporary directoryapp.use('/temp-api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));// swagger-ui-express middleware that redirect user to /api-docs will not be aware the prefix of path by ngnixconst apiDocsRedirectPath = "application/prefix/go/here".concat('/temp-api-docs/');app.get('/api-docs', function(req, res) {    res.redirect(apiDocsRedirectPath);});