Load balancing two wordpress containers sites with nginx docker

  containers, docker, load-balancing, nginx, wordpress

I am trying to load balance two wordpress websites with nginx.
When i go to port 8080 on the VM that holds the containers it just redirects me to http://docker-nginx:8000/9000/ port and not the actual wordpress website.

Accessing the websites by their port (9000 or 8000) works perfectly.

reverseproxy and mysql_image are custom images.

dockerfile for reverseproxy image:

FROM nginx:alpine

COPY nginx.conf /etc/nginx/nginx.conf

RUN apk update && apk add bash

nginx.conf

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    upstream docker-nginx {
        server wordpress:80;
        server second_wordpress:80;
    }

    server {
        listen 8080;

        location / {
            proxy_pass         http://docker-nginx;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}

docker-compose.yml

version: '3.3'

services:
   reverseproxy:
     image: reverseproxy
     ports:
       - "8080:8080"
     restart: always

   db:
     image: mysql_image
     ports:
       - "3306:3306"
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
       - reverseproxy
     image: wordpress:latest
     ports:
      - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: root
       WORDPRESS_DB_PASSWORD: somewordpress
       WORDPRESS_DB_NAME: wordpress
     volumes:
       - wordpress:/var/www/html

   second_wordpress:
     depends_on:
       - db
       - reverseproxy
     image: wordpress:latest
     ports:
      - "9000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: root
       WORDPRESS_DB_PASSWORD: somewordpress
       WORDPRESS_DB_NAME: wordpress2
     volumes:
       - wordpress2:/var/www/html

volumes:
    wordpress:
    wordpress2:

Source: Docker Questions

LEAVE A COMMENT