Answer a question

I use swagger-ui-express package(https://github.com/scottie1984/swagger-ui-express) (Node.js) and work fine with this config:

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

when directly got to /api-docs every thing is fine, but when i come from nginx for example host/myApp/api-docs redirect me to host/api-docs and it's obvious that after redirect I get 404

Answers

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-docs and redirect to correct path, now application route and swagger works fine.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐