为什么找不到反向代理后面的Swagger端点?

时间:2018-03-27 19:36:49

标签: nginx asp.net-core swagger swagger-ui swashbuckle

我有少量的ASP.NET核心服务,都在Docker中运行(通过Docker Compose)。所有服务当前都使用带前缀的路由(他们自己的服务名称)。并且它们都在Docker Compose中设置为使用自己的服务名称作为主机名(服务容器之间的连接正常)。

/api-docs端点由Swashbuckle提供;我们也在这里设置了前缀路径。

app.UseSwagger(options =>
{
    options.RouteTemplate = "scheduler/api-docs/{documentName}/swagger.json";
});

app.UseSwaggerUI(options =>
{
    options.RoutePrefix = "scheduler/api-docs";
    options.SwaggerEndpoint("/scheduler/api-docs/v1/swagger.json", "Scheduler API v1");
});

我正在尝试在容器网络中配置Nginx反向代理,以便我可以去,比如说......

http://localhost/<service-name>/api-docs

它将在容器网络内重定向到...

http://<service-name>:5000/<service-name>/api-docs

所以,这是我提出的Nginx配置...基本上,匹配请求URI的第一部分,应该是服务名称,并且代理到名为相同的主机,Nginx 应该< / em>自动添加$request_uri

server {
    listen 80;

    location ~* ^/(?<target>.+)/ {
        proxy_pass http://$target:5000;
        proxy_redirect off;
        resolver 127.0.0.11;
    }
}

这是我为/scheduler/healthcheck端点获得的内容。一切都好!

api-gateway_1               | 172.19.0.1 - - [27/Mar/2018:17:50:24 +0000] "GET /scheduler/healthcheck HTTP/1.1" 200 491 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"

但是,当我试图去/scheduler/api-docs时,我遇到了问题。我们到达服务容器,Swashbuckle执行从/scheduler/api-docs/scheduler/api-docs/的301重定向。

api-gateway_1               | 172.19.0.1 - - [27/Mar/2018:17:51:18 +0000] "GET /scheduler/api-docs HTTP/1.1" 301 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"

然后,事情变得糟糕......我们“失去”了路线的/scheduler部分!

api-gateway_1               | 172.19.0.1 - - [27/Mar/2018:17:51:18 +0000] "GET /scheduler/api-docs/ HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
api-gateway_1               | 2018/03/27 17:51:18 [error] 5#5: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: , request: "GET /scheduler/api-docs/ HTTP/1.1", upstream: "http://172.19.0.5:80/api-docs:5000", host: "localhost:4000"

为什么Swashbuckle重定向通过Nginx发回请求,我认为这都将由本地服务处理,为什么Nginx会从此请求中剥离必要的路由前缀?

我该如何表现??

我试图重建“整个”URI,只是为了看看会发生什么......

-   proxy_pass http://$target:5000;
+   proxy_pass http://$target:5000$request_uri;

这让甚至更糟糕了!

api-gateway_1               | 172.19.0.1 - - [27/Mar/2018:18:03:48 +0000] "GET /scheduler/api-docs HTTP/1.1" 301 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
api-gateway_1               | 172.19.0.1 - - [27/Mar/2018:18:03:48 +0000] "GET /scheduler/api-docs/ HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
api-gateway_1               | 2018/03/27 18:03:48 [error] 5#5: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: , request: "GET /scheduler/api-docs/ HTTP/1.1", upstream: "http://172.19.0.5:80/api-docs:5000/scheduler/api-docs/", host: "localhost:4000"

仅供参考,如果我直接访问网站(通过Docker等发布端口后),我的浏览器一切正常。

1 个答案:

答案 0 :(得分:0)

嗯...问题是一个“贪婪”的正则表达式。正如定义的那样,正则表达式捕获组.+将使用所有直到 last 正斜杠。

enter image description here

您应该使用“懒惰”正则表达式捕获组.+?来捕获两个正斜杠之间的单个URI段,而不是所有 last >前进斜线!

enter image description here