Three CentOS 7 virtual machines are used to build a basic Nginx reverse proxy load balancing cluster:
- 192.168.2.76: Nginx load balancer
- 192.168.2.82: Web server (web01)
- 192.168.2.78: Web server (web02)
Install Nginx on All Nodes
Install required dependencies:
yum -y install openssl openssl-devel pcre pcre-devel gcc wget
Create the installation directory and download Nginx:
mkdir /app
cd /app
wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
useradd -s /sbin/nologin -M nginx
id nginx
tar xf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --user=nginx --group=nginx --prefix=/app/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
Configure Backand Web Servers (web01 and web02)
Edit /app/nginx/conf/nginx.conf:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name bbs.dengchuanghai.org;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/access_bbs.log main;
}
}
Create document root and add identifying content:
On web01 (192.168.2.82):
mkdir -p /app/nginx/html/bbs
echo "192.168.2.82 bbs" > /app/nginx/html/bbs/index.html
echo "192.168.2.82 bbs.dengchuanghai.org" >> /etc/hosts
On web02 (192.168.2.78):
mkdir -p /app/nginx/html/bbs
echo "192.168.2.78 bbs" > /app/nginx/html/bbs/index.html
echo "192.168.2.78 bbs.dengchuanghai.org" >> /etc/hosts
Valiadte and start Nginx:
/app/nginx/sbin/nginx -t
/app/nginx/sbin/nginx
ss -tnlp | grep 80
Configure the Load Balancer (192.168.2.76)
Insure vim is installed:
yum -y install vim-enhanced
Edit /app/nginx/conf/nginx.conf:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream backend_pool {
server 192.168.2.82:80 weight=1;
server 192.168.2.78:80 weight=1;
}
server {
listen 80;
server_name www.dengchuanghai.org;
location / {
proxy_pass http://backend_pool;
}
}
}
Add host entry for local resolution:
echo "192.168.2.76 www.dengchuanghai.org" >> /etc/hosts
Test configuration and launch Nginx:
/app/nginx/sbin/nginx -t
/app/nginx/sbin/nginx
Access the service via a browser using the load balancer’s IP address or configured domain.