Setting Up Nginx and Keepalived High‑Availability on Multiple Servers
This guide walks through preparing four servers, installing and configuring Nginx as a load balancer, setting up Keepalived for virtual IP failover, and verifying that traffic seamlessly switches between the master and backup nodes.
The environment consists of four servers: 192.168.210.85 (Nginx primary/Keepalived master), 192.168.210.177 (Nginx secondary/Keepalived backup), and two backend servers 192.168.210.176 and 192.168.210.195.
Nginx installation is performed on all four machines. The main Nginx configuration (/etc/nginx/nginx.conf) is as follows:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr- $remote_user [$time_local] "$request"''$status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
upstream back {
server 192.168.210.176:80;
server 192.168.210.195:80;
}
server {
listen 80;
server_name www.sfqd.com;
location / {
proxy_pass http://back;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}After editing, Nginx is started on each host.
Keepalived installation follows, with the service started via systemctl start keepalived and its status checked with systemctl status keepalived. The configuration file (/etc/keepalived/keepalived.conf) on the master (192.168.210.85) looks like:
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.210.11
}
}The backup node (192.168.210.177) uses a similar file with state BACKUP and a lower priority (150):
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.210.11
}
}Both nodes are started with systemctl start keepalived. The virtual IP (VIP) 192.168.210.11 should be present on the master; after stopping Keepalived on the master ( systemctl stop keepalived), the VIP migrates to the backup node, confirming failover works.
On the client machine, add an entry to /etc/hosts mapping 192.168.210.11 www.sfqd.com. Accessing http://www.sfqd.com in a browser now routes traffic through Nginx, which load‑balances between the two backend servers. The test confirms that the VIP moves without interruption, completing the Keepalived + Nginx high‑availability setup.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
