Operations 9 min read

How to Build Nginx High Availability with Keepalived on CentOS

This guide walks through preparing two CentOS VMs, installing Nginx, explaining high‑availability concepts, and configuring Keepalived with VRRP and a health‑check script to achieve automatic failover of Nginx services via a virtual IP.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build Nginx High Availability with Keepalived on CentOS

Preparation

192.168.16.128

192.168.16.129

Two virtual machines with Nginx installed.

Install Nginx

Update yum repository:

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

Install Nginx: yum -y install nginx Control commands:

systemctl start nginx; # start Nginx
systemctl stop nginx; # stop Nginx

What is High Availability?

High Availability (HA) is a design factor in distributed systems that aims to minimize downtime. Perfect 100% availability is impossible, so we strive to reduce service interruptions.

Problem addressed

In production, Nginx is often used as a reverse proxy. If Nginx crashes or the server goes down, all external interfaces become inaccessible.

Although we cannot guarantee 100% uptime, we can mitigate failures using Keepalived to achieve Nginx high availability.

Dual‑machine hot standby solution

This is a common HA pattern: one server provides the service while the other stands by to take over if the primary fails.

What is Keepalived?

Keepalived was originally designed for LVS load balancing, managing and monitoring service nodes. It later added VRRP support, allowing it to provide HA for services such as Nginx, HAProxy, MySQL, etc.

Failover mechanism

Keepalived uses VRRP. The Master node sends heartbeat messages to the Backup. If the Master fails, the Backup detects the missing heartbeat, takes over the virtual IP and services. When the Master recovers, the Backup releases the IP and returns to standby.

Implementation steps

Install Keepalived

Install via yum, which resolves dependencies automatically: yum -y install keepalived Modify keepalived.conf on the primary host (192.168.16.128). The file is generated under /etc/keepalived:

# Check script
vrrp_script chk_http_port {
    script "/usr/local/src/check_nginx_pid.sh" # script to check if nginx is running
    interval 2   # seconds
    weight 2
}
# VRRP instance definition
vrrp_instance VI_1 {
    state MASTER
    interface ens33   # network interface, check with ifconfig
    virtual_router_id 66
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_http_port
    }
    virtual_ipaddress {
        192.168.16.130
    }
}

The virtual_ipaddress defines the VIP used to access the service. The interface must match the server's NIC (check with ip addr). The same authentication settings must be applied on the backup.

Modify keepalived.conf on the backup host (192.168.16.129):

# Check script
vrrp_script chk_http_port {
    script "/usr/local/src/check_nginx_pid.sh"
    interval 2
    weight 2
}
# VRRP instance definition
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 66
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_http_port
    }
    virtual_ipaddress {
        192.168.16.130
    }
}

Health‑check script ( check_nginx_pid.sh):

#!/bin/bash
# Check if nginx is running
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ]; then
    # start nginx if not running
    systemctl start nginx
    if [ `ps -C nginx --no-header | wc -l` -eq 0 ]; then
        # if restart fails, stop keepalived to trigger VIP failover
        killall keepalived
    fi
fi

Make the script executable: chmod 775 check_nginx_pid.sh Test failover: Access the VIP 192.168.16.130; initially it shows the page from 192.168.16.128 (primary). Stop nginx on the primary; the script restarts it, so the VIP still points to the primary. Shut down the primary host; the VIP now resolves to 192.168.16.129, demonstrating automatic failover.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

high availabilityLinuxfailoverCentOSVRRPkeepalived
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.