nginx - 如何更改默认的 swagger.json 文件路径?

标签 nginx swagger swagger-ui swagger-2.0 consul

我创建了 Ngnix-Consul Docker 设置,引用 https://github.com/nginxinc/NGINX-Demos/tree/master/consul-template-demo .

并创建了许多微服务。因此,只有在添加服务名称后才能访问所有微服务,例如

http://example.com/service_name/get_data

一切正常,然后我想为所有微服务添加 swagger,因此尝试使用以下代码段
我可以通过使用访问 swagger ui
http://example.com/service_name/ui

但问题是我无法在该 ui 中加载 swagger.json,因为它试图访问以下 url 上的 swagger.json
http://example.com/swagger.json

但 json 文件已打开
http://example.com/service_name/swagger.json    

如何更改 swagger.json 的默认路径?

微服务中的应用程序是在 python-flask 中创建的
我试过下面的片段
swagger: "2.0"
    info:
      description: "Add service"
      version: "1.0.0"
      title: "Add Service"
      contact:
        email: "abc@efg.com"
      license:
        name: "s1.0"
        url: "http://sample.com"
    host: "abc.efg.com"
    tags:
    - name: "add service"
      description: "service"
    - name: "delete service"
      description: "data"
    schemes:
    - "http"
    paths:
      /service_name/get_data:

甚至我也尝试在 swagger.yaml 文件中添加 basePath
然后它甚至没有打开swaggerui
swagger: "2.0"
    info:
      description: "Add service"
      version: "1.0.0"
      title: "Add Service"
      contact:
        email: "abc@efg.com"
      license:
        name: "s1.0"
        url: "http://sample.com"
    host: "abc.efg.com"
    basePath: "service_name"
    tags:
    - name: "add service"
      description: "service"
    - name: "delete service"
      description: "data"
    schemes:
    - "http"
    paths:
      /get_data:

更新:
from flask import Flask
import connexion

app = Flask(__name__)
app = connexion.App(__name__)
app.add_api('swagger.yaml')

//apis

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8090, debug=True)

最佳答案

我自己也有类似的问题。我的解决方案是在 NGINX 级别禁用路径重写,以便微服务接收完整的 url:

前:

Request: 
http://example.com/service_name/get_data

Service sees:
/get_data

后:
Request: 
http://example.com/service_name/get_data

Service sees:
/service_name/get_data

只有这样,您才能在 swagger.yaml 文件中将 basePath 指定为“service_name”:
swagger: "2.0"
    info:
      description: "Add service"
      version: "1.0.0"
      title: "Add Service"
    host: "abc.efg.com"
    basePath: "service_name"

关于nginx - 如何更改默认的 swagger.json 文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50423858/

相关文章:

mysql - sudo systemctl start 返回 "Failed to wait for response: Success"

swagger - 在 swagger 中,是否可以在一个文件中导入多个 yaml 文件?

spring - Swagger codegen - 更改 api 和 Controller 类名称

Java + Spring Boot + REST 安全问题

php - Symfony NelmioApiDocBundle swagger-ui PHP 注释 json 对象,对象属性数组未显示

rest - 为 Swagger 写一个 api-doc

elasticsearch - filebeats 的 Nginx 模块不解析访问日志

linux - Nginx Proxy 下多个位置多个服务器

http - nginx auth_basic "Restricted"在每次请求时提示登录

rest - Swagger 和 JSON 补丁