Docker Community Forums

Share and learn in the Docker community.

Nginx:latest as reverse proxy for multiple web apps

I want to containerize my web applications. Currently, I am using Apache to provide a couple of PHP apps.

Every app should be provided by their own container. Nginx should be reachable by port 80/443.

Depending on the subroute it should proxying to one of the containers.

Everything should be orchestrated by Kubernetes on Azure.

I already checked nginx:jwilder. It´s very close to my use-case. But the apps should be reached by routes instead of subdomains:

kubernetes-url/app1 => htttp://app1:80
kubernetes-url/app2 => htttp://app2:80

Here is my first try:

.
|

  • – app1
    | – Dockerfile
    | – index.html
  • – app2
    | – Dockerfile
    | – index.html
  • – docker-compose-yml
  • – nginx.conf

docker-composer.yml:

version: '3'
services:
        nginx:
                image: nginx:latest
                container_name: reverse_proxy
                volumes:
                        - ./nginx.conf:/etc/nginx/nginx.conf
                        #- ./hmtl:/usr/share/nginx/html
                ports:
                        - "80:80"
                        - "443:443"
                restart: always
                networks:
                        - app-network

        app1:
                build: ./app1
                image: app1
                container_name: app1
                restart: always
                expose:
                        - "80"
                depends_on:
                        - nginx
                networks:
                        - app-network

        app2:
                build: ./app2
                image: app2
                container_name: app2
                restart: always
                expose:
                        - "80"
                depends_on:
                        - nginx
                networks:
                        - app-network


#Docker Networks
networks:
  app-network:

nginx.conf:

events {

}

http {
        error_log /etc/nginx/error_log.log warn;
        client_max_body_size 20m;

        proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;

        server {
                server_name wudio.de;

                location / {
                        proxy_pass http://app2:80;
                        rewrite ^/app2(.*)$ $1 break;
                }

                location /app1 {
                        proxy_pass http://app1:80;
                        rewrite ^/app1(.*)$ $1 break;
                }

                location /app2 {
                        proxy_pass http://app2:80;
                        rewrite ^/app2(.*)$ $1 break;
                }
        }
}

app1/Dockerfile:

FROM nginx:alpine
COPY . /usr/share/nginx/html

app1/index.html:

<h1>hello world</h1>
<p>test 1</p>

Unfortunately, the locations don’t work. If I curl for www.kubernetes-url.com I get the expected result of app1.
But if I query for example www.kubernetes-url.com/app1 it doesn’t work (website not reachable). What is wrong?