Operations 7 min read

Implementing High Availability for Nginx with Keepalived: A Step‑by‑Step Guide

This guide explains how to achieve high availability for Nginx using Keepalived by setting up a double‑machine hot‑standby configuration, installing necessary packages, configuring VRRP‑based failover, writing health‑check scripts, and testing automatic VIP takeover when the primary server fails.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Implementing High Availability for Nginx with Keepalived: A Step‑by‑Step Guide

High Availability (HA) refers to designing distributed systems to minimize downtime, ensuring services remain accessible as much as possible.

The article addresses the problem of Nginx failure in production, where a single point of failure can render all external interfaces inaccessible, and proposes using Keepalived to achieve HA.

It explains the double‑machine hot‑standby model, where one server actively provides the service while a second server stands by to take over automatically upon failure.

Keepalived, originally built for LVS, provides VRRP‑based failover and can manage services such as Nginx, HAProxy, MySQL, etc.

The implementation steps include preparing two virtual machines (192.168.16.128 and 192.168.16.129) with Nginx installed, installing Keepalived via yum, and configuring the keepalived.conf files on both master and backup nodes.

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
yum -y install nginx
systemctl start nginx; #启动Nginx
systemctl stop nginx; #停止Nginx
yum -y install keepalived
# keepalived.conf (master)
vrrp_script chk_http_port {
    script "/usr/local/src/check_nginx_pid.sh"
    interval 2
    weight 2
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    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
    }
}
# keepalived.conf (backup)
vrrp_script chk_http_port {
    script "/usr/local/src/check_nginx_pid.sh"
    interval 2
    weight 2
}
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
    }
}
#!/bin/bash
#检测nginx是否启动了
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ]; then
    systemctl start nginx
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ]; then
        killall keepalived
    fi
fi
chmod 775 check_nginx_pid.sh

After configuring the virtual IP (VIP) 192.168.16.130, accessing the VIP initially serves the master node; stopping Nginx on the master triggers the script to restart it, and if the master goes down, the backup node automatically takes over the VIP, demonstrating a complete enterprise‑grade HA solution.

The article notes that Keepalived also supports additional features such as email alerts, which can be explored in its official documentation.

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 availabilityNGINXSystem Administrationfailoverkeepalived
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

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.