Home > Mobile >  How to serve multiple container using bitnami nginx container
How to serve multiple container using bitnami nginx container

Time:03-11

I have containers in one server:

  1. web-zamrud, api-zamrud and db-zamrud, all of them using docker bridge named zamrud-network

  2. web-berlian, api-berlian and db-berlian, all of them using docker bridge named berlian-network

  3. nginx container to serve web-zamrud and web-berlian.

Below is zamrud containers docker-compose

services:
  api-zamrud:
    image: registry.gitlab.com/zamrud/zamrudapi
    container_name: api-zamrud
    networks:
      - zamrud-network

  web-zamrud:
    image: registry.gitlab.com/zamrud/zamrudweb
    container_name: web-zamrud
    networks:
      - zamrud-network

networks:
  zamrud-network:
    external: true

Below is berlian container docker-compose

services:
  api-berlian:
    image: registry.gitlab.com/berlian/berlianapi
    container_name: api-berlian
    networks:
      - berlian-network

  web-berlian:
    image: registry.gitlab.com/berlian/berlianweb
    container_name: web-berlian
    networks:
      - berlian-network

networks:
  berlian-network:
    external: true

Below is bitnami nginx docker compose file

services:
  nginx:
    image: docker.io/bitnami/nginx:1.21
    container_name: nginx
    volumes:
      - ./conf/zamrud.conf:/opt/bitnami/nginx/conf/server_blocks/zamrud.conf:ro
      - ./conf/berlian.conf:/opt/bitnami/nginx/conf/server_blocks/berlian.conf:ro
    networks:
      - zamrud-network
      - berlian-network
    ports:
      - "80:80"

Below is zamrud.conf

server {
    listen 80;
    listen [::]:80;

    server_name zamrud.com www.zamrud.com;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://web-zamrud;
        proxy_redirect off;
    }

    location /api {
      rewrite /api/(.*) /$1 break;

      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header HOST $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://api-zamrud:5000;
      proxy_redirect off;
    }
}

Below is berlian.conf

server {
    listen 80;
    listen [::]:80;

    server_name berlian.com www.berlian.com;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://web-berlian;
        proxy_redirect off;
    }

    location /api {
      rewrite /api/(.*) /$1 break;

      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header HOST $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://api-berlian:5000;
      proxy_redirect off;
    }
}

If I try to access www.zamrud.com, the content of website is properly displayed.

But if I try to access www.berlian.com, it show content of www.zamrud.com.

Am I missing configuration in nginx?

CodePudding user response:

In case anyone face same problem, what I did is make the bitnami nginx container down

docker-compose down

and then up the bitnami nginx container again

docker-compose up -d

Note:

file name for bitnami nginx docker-compose is docker-compose.yaml

I run the docker down and up in the same folder as docker-compose.yaml

  • Related