Nginx microservice gateway config within kubernetes cluster

8/18/2018

Basically we have a set of microservices we have deployed to a kubernetes cluster hosted in AWS. We would like to run these through a gateway configuration in 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} . I'm always getting a 404 when I try hitting these endpoints through postman, which tells me it can't resolve these mappings.

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)-

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.

-- Brandon
.net-core
kubernetes
nginx

1 Answer

8/18/2018

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). Following https://gist.github.com/soheilhy/8b94347ff8336d971ad0 you could re-write that target like

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