Skip to content

CentOS 8: How to Setup Highly Available NGINX by KeepAlived?

Updated 2 min read

Updated: 28 September 2026 · Applies to: AlmaLinux 9 / Rocky Linux 9 (Keepalived, Nginx)

CentOS 8 is end of life; these steps are for AlmaLinux 9 / Rocky Linux 9 (Ubuntu works the same). Keepalived moves a shared virtual IP between two Nginx servers: if the active one fails, the backup takes over the IP within seconds.

Note: The virtual IP must be allowed to move between your servers. On a VPS or cloud network this needs a failover or additional IP that the provider lets you assign to both servers; ask our support team before you set it up.

1. Install on both servers

dnf install keepalived nginx        # apt install keepalived nginx on Ubuntu
systemctl enable --now nginx

2. Configure the first server (MASTER)

In /etc/keepalived/keepalived.conf:

vrrp_script chk_nginx {
    script "/usr/bin/systemctl is-active --quiet nginx"
    interval 2
    fall 2
    rise 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    unicast_src_ip 192.0.2.11
    unicast_peer {
        192.0.2.12
    }
    authentication {
        auth_type PASS
        auth_pass S3cret12
    }
    virtual_ipaddress {
        192.0.2.100/24
    }
    track_script {
        chk_nginx
    }
}

3. Configure the second server (BACKUP)

Use the same file with state BACKUP, a lower priority (for example 100), and the two IPs swapped in unicast_src_ip and unicast_peer.

4. Start and test

firewall-cmd --permanent --add-rich-rule='rule protocol value="vrrp" accept' && firewall-cmd --reload
systemctl enable --now keepalived
ip addr show eth0            # the MASTER shows 192.0.2.100

Stop Nginx on the master (systemctl stop nginx): within a few seconds the virtual IP appears on the backup. Point your DNS at the virtual IP, and keep the website files and configuration the same on both servers.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket