0
votes

I have several apps with multiple containers running, organized in some docker-compose files, running on different ports. Actually access them like example.com:port.

Example:

  • app1 with 2 services exposing port 1111 I access trough example.com:1111
  • app2 with 2 services exposing port 3333 I access trough example.com:3333

Now I want to access them with example.com/app1, example.com/app2 instead. I thought to realize this with another container running nginx. But I don't get how to write the nginx configuration to achieve this.

2

2 Answers

0
votes

Something like this for your nginx.conf

server {
    listen 80;

    location /app1 {
        proxy_pass  http://example.com:1111;
    }

    location /app2 {
        proxy_pass  http://example.com:3333;
    }
}

Although remember if your using named services in your docker-compose then you can just use those names as they will be available in the default network. e.g.

server {
    listen 80;

    location /app1 {
        proxy_pass  http://app1:1111;
    }

    location /app2 {
        proxy_pass  http://app2:3333;
    }
}

Also bear in mind nginx is a pain to configure via environment variables without using lua which is a bit of a struggle with docker IIRC so it will be best just to hard code it.

0
votes

I know, you asked directly for Nginx as ingress controller, but you would like to have a look at traefik.io. This is a reverse-proxy/load-balancer which is meant to be used with dynamic backends.

Doing the same with Nginx is not an easy task as you have to know how networking and service resolution is handled by Docker.