我使用 swagger-ui-express package( https://github.com/scottie1984/swagger-ui-express) (Node.js) 并且在这个配置中工作正常:
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
app.use('/api-docs',swaggerUi.serve, swaggerUi.setup(swaggerDocument));
Run Code Online (Sandbox Code Playgroud)
当直接进入 /api-docs 时,一切都很好,但是当我来自 nginx 时,例如host/myApp/api-docs将我重定向到host/api-docs,很明显,重定向后我得到 404
问题在于swagger-ui-express中间件将用户重定向到 host/api-docs 并且不使用路径的前缀,所以我用一个技巧解决了这个问题,我在这个路径中使用了中间件:
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
app.use('/app-prefix/api-docs',swaggerUi.serve, swaggerUi.setup(swaggerDocument));
Run Code Online (Sandbox Code Playgroud)
在 nginx 中我定义了两个位置:
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/;
}
Run Code Online (Sandbox Code Playgroud)
因此,当用户请求 nginx 时,nginx 将其路由到应用程序的第二条路径:
/app-prefix/api-docs
在那之后 swagger middlware 将它重定向到host/app-prefix/api-docs
并重定向到正确的路径,现在应用程序路由和 swagger 工作正常。
| 归档时间: |
|
| 查看次数: |
7553 次 |
| 最近记录: |