This extension is dedicated to Dr. Maleki, the Vice Dean of Education of the Faculty of Electrical and Computer Engineering at Isfahan University of Technology. Her strategy for equalizing the capacity of course groups inspired me to develop this extension.
This is a dynamic module for Nginx that introduces a new upstream load balancing algorithm named dr_maleki.
This algorithm is designed for scenarios where you want to sequentially fill up servers to a certain capacity before moving to the next, and then dynamically raise that capacity for all servers once they are full.
The logic of the dr_maleki algorithm is as follows:
- An initial request
limitis set for all upstream servers (e.g., 100 active connections). - All new requests are sent to the first server in the upstream list until its connection count reaches the
limit. - Nginx then moves to the second server and sends all new requests there until it also reaches the
limit. - This process continues sequentially for all servers in the list.
- Once all servers have reached the current
limit, thelimitfor all servers is increased by 10%. - The entire process repeats, starting again from the first server with the new, higher limit.
Before compiling, you need to have the following installed on your system:
- The Nginx source code. The version of the source code must match the version of Nginx you have installed.
- A C compiler and build tools (e.g.,
gcc,make). - The development libraries required to build Nginx from source (PCRE, OpenSSL, zlib, etc.).
You can install the basic build tools with:
# For Debian / Ubuntu
sudo apt-get install -y build-essential
# For CentOS / RHEL / Fedora
sudo yum groupinstall -y "Development Tools"Follow these steps carefully to compile and install the module.
Clone this repository to your machine.
git clone https://github.com/hamidreza2005/dr-maleki-balancer
cd dr_maleki_balancerDownload the Nginx source code that exactly matches your installed Nginx version.
First, check your current Nginx version:
nginx -vThen, download the matching source. For example, if you have version 1.24.0:
cd /tmp
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gzFor binary compatibility, a dynamic module must be compiled with the same flags as the Nginx binary it will be loaded into. Get your current Nginx configuration flags by running:
nginx -VCopy the entire output line that begins with configure arguments:.
Navigate to the Nginx source directory and run the ./configure script with the arguments from the previous step, adding this module at the end.
cd /tmp/nginx-1.24.0/
# PASTE your configure arguments and add the --add-dynamic-module flag
./configure [PASTE_ARGUMENTS_HERE] --add-dynamic-module=/path/to/dr_maleki_balancer
# Compile the modules
make modulesExample:
./configure --prefix=/etc/nginx --with-http_ssl_module --with-compat --add-dynamic-module=/home/user/dr_maleki_balancerCopy the compiled module (.so file) to your Nginx modules directory. The location may vary, but it's often /usr/lib/nginx/modules/.
sudo cp objs/ngx_http_upstream_dr_maleki_module.so /usr/lib/nginx/modules/Finally, enable the module in your nginx.conf file.
Add the following line at the top of your /etc/nginx/nginx.conf file.
load_module modules/ngx_http_upstream_dr_maleki_module.so;In the http section of your configuration, define an upstream block and activate the algorithm by adding the dr_maleki; directive.
http {
upstream my_backend {
# Activate the Dr. Maleki load balancing algorithm
dr_maleki;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://my_backend;
}
}
}Test your configuration and restart the Nginx service.
sudo nginx -t
sudo systemctl restart nginxYour custom load balancer is now active!
This project is licensed under the MIT License.