简体   繁体   English

kubernetes 集群中的 Nginx 微服务网关配置

[英]Nginx microservice gateway config within kubernetes cluster

Basically we have a set of microservices we have deployed to a kubernetes cluster hosted in AWS.基本上,我们有一组微服务已部署到托管在 AWS 中的 kubernetes 集群。 We would like to run these through a gateway configuration in nginx.我们想通过 nginx 中的网关配置来运行这些。

Our current configuration which doesn't work looks something like this-我们当前不起作用的配置看起来像这样-

upstream some-api1 {
    server some-api1:80;
}

upstream some-api2 {
    server some-api2:80;
}

upstream some-api3 {
    server some-api3:80;
}

server {
    listen 80;
    server_name gateway.something.com;
    location /api1 {
        proxy_pass  http://some-api1;
    }
    location /api2 {
        proxy_pass  http://some-api2;
    }
    location /api3 {
        proxy_pass  http://some-api3;
    }
}

Our services have been built with dotnet core, so the underlying urls would be something like http://some-api1/ {api/controllername} .我们的服务是使用 dotnet 核心构建的,因此底层 url 将类似于http://some-api1/ {api/controllername} 。 I'm always getting a 404 when I try hitting these endpoints through postman, which tells me it can't resolve these mappings.当我尝试通过邮递员访问这些端点时,我总是收到 404,这告诉我它无法解析这些映射。

However I'm able to access an api within the cluster using an explicit config for an api like so(which is what I don't want to do)-但是,我可以使用像这样的 api 的显式配置来访问集群中的 api(这是我不想做的)-

server {
    listen 80;
    server_name someapi1.something.com;
    location /{
        proxy_pass http://some-api1;
    }   
}..

If someone could shed some light on what's wrong with the configuration or recommend the best approach for this it would be greatly appreciated.如果有人可以阐明配置的问题或为此推荐最佳方法,我们将不胜感激。

As @Luminance suggests, you're seeing traffic to /api1 go to some-api1/api1 instead of just some-api1 on the base path for that target service (which is what your app would respond to).正如@Luminance 所建议的那样,您会看到 /api1 的流量转到 some-api1/api1 而不是该目标服务的基本路径上的 some-api1(这是您的应用程序将响应的内容)。 Following https://gist.github.com/soheilhy/8b94347ff8336d971ad0 you could re-write that target likehttps://gist.github.com/soheilhy/8b94347ff8336d971ad0之后,您可以重新编写该目标,例如

location /api1 {
       rewrite ^/api1(.*) /$1 break;
       proxy_pass http://some-api1;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM