Overview:
The main approach here is to use NGINX as a load balancer to distribute load between multiple instance of apache server propped up using docker-compose. Here the NGINX will be handing all the traffic through port 80 and distributing it throughout the different available replicas of the apache server. Our approach is to serve the application on port 8080 for all apache server instances and on NGINX setup all the upstream servers to hit on port 8080. This way instead having to give a port range for the apache instances we are simply exposing port 8080 and then using the NGINX load balancer to distribute load. Note: one reason of not using port range on the apache 2.4 is on docker-compose version 2+ it appears to give a breaking error. But works fine for docker-compose < 2.0. Where you are just given a warning.
version: '3.9'
services:
nginx:
image: nginx:latest
restart: always
ports:
- "80:80"
environment:
- NGINX_HOST=localhost
- NGINX_PORT=80
volumes:
- ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
links:
- apache
apache:
image: httpd:latest
restart: always
expose:
- 8080
volumes:
- ./config/httpd.conf:/usr/local/apache2/conf/httpd.conf
- ./config/host.conf:/usr/local/apache2/conf/extra/httpd-vhosts.conf
- ./www:/app
deploy:
replicas: 2
Changes made in the httpd.conf are :
Listen 8080
Changes made in the httpd-vhosts.conf are:
<VirtualHost *:8080>
ServerName localhost
ServerAdmin thephpx@gmail.com
DocumentRoot /app
<Directory "/app">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
Configuration set on nginx.conf file are:
user nginx;
events {
worker_connections 1000;
}
http {
upstream backend {
server apache_1:8080;
server apache_2:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend;
}
}
}
In case you want to increase the number of apache server instances to more than 2, then you have to first scale them and then create entry for each server on this nginx.conf file upstream server list and then build the nginx instance again.
Leave a comment