Home Operation and Maintenance Nginx Nginx high availability method

Nginx high availability method

May 31, 2023 pm 03:04 PM
nginx

Preparation:

Nginx high availability method

​192.168.16.128

​192.168.16.129

Two virtual machines. Install Nginx

Install Nginx

Update yum source file:

rpm-ivhhttp://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

​wget-O/etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-7.repo

Install Nginx:

​ yum-yinstallnginx

Operation command:

systemctlstartnginx;#Start Nginx

systemctlstopnginx;#stopNginx

What is high availability?

High availability HA (HighAvailability) is one of the factors that must be considered in the design of distributed system architecture. It usually refers to reducing the time when the system cannot provide services through design. If a system can always provide services, then the availability is 100%, but there are unforeseen circumstances. So we can only try to reduce service failures as much as possible.

solved problem?

In production environments, Nginx is often used as a reverse proxy to provide external services. However, Nginx will inevitably encounter failures one day, such as server downtime. When Nginx goes down, all externally provided interfaces will become inaccessible.

Although we cannot guarantee that the server is 100% available, we must find ways to avoid this tragedy. Today we use keepalived to implement Nginx

High availability.

Dual-machine hot backup solution

This solution is the most common high-availability solution among domestic enterprises. Dual-server hot backup actually means that one server is providing services and the other is in standby state for a certain service. When one server is unavailable, the other one Will take his place.

What is keepalived?

Keepalived software was originally designed for LVS load balancing software to manage and monitor the status of each service node in the LVS cluster system. Later, it added the VRRP (Virtual Router Redundancy Protocol) function that can achieve high availability. Therefore, in addition to managing LVS software, Keepalived can also be used as a high-availability solution software for other services (such as Nginx, Haproxy, MySQL, etc.)

Failover mechanism

Failover transfer between Keepalived high-availability services is implemented through VRRP.

When the Keepalived service is working normally, the main Master node will continuously send (multicast) heartbeat messages to the backup node to tell the backup Backup node that it is still alive. When the main Master node fails, it cannot send heartbeat messages and the backup node will not be able to send heartbeat messages. Therefore, the node can no longer detect the heartbeat from the master node, so it calls its own takeover program to take over the IP resources and services of the master node. When the master node recovers, the backup node will release the IP resources and services it took over when the master node failed, and return to its original backup role.

Implementation process

Install keepalived

You can install it directly using yum. This method will automatically install dependencies:

​ yum-yinstallkeepalived

Modify the host (192.168.16.128) keepalived configuration file

If installed in yum mode, the configuration file will be produced under /etc/keepalived:

vikeepalived.conf

keepalived.conf:

​ #Detection script

vrrp_scriptchk_http_port{

Script"/usr/local/src/check_nginx_pid.sh"#Heartbeat execution script to detect whether nginx is started

interval2# (interval for detecting script execution, unit is seconds)

weight2#weight

}

​#vrrp instance definition part

vrrp_instanceVI_1{

stateMASTER#Specify the role of keepalived, MASTER is the main one, and BACKUP is the backup one

interfaceens33#The current network interface card for vrrp communication (current centos network card) uses ifconfig to check your specific network card

​virtual_router_id66#Virtual routing number, the master and slave must always be

priority100#Priority, the larger the value, the higher the priority of obtaining and processing the request

advert_int1#Checking interval, the default is 1s (vrrp multicast cycle seconds)

​#Authorized access

Authentication{

auth_typePASS#Set the authentication type and password. MASTER and BACKUP must use the same password for normal communication

auth_pass1111

}

track_script{

chk_http_port# (call detection script)

}

virtual_ipaddress{

192.168.16.130#Define virtual ip (VIP), you can set more than one, one per line

}

}

​Vip can be configured in virtual_ipaddress, and services can be accessed online through vip.

The interface needs to be set according to the server network card. The usual viewing method is ipaddr

Authentication configuration authorization access to the backup machine also requires the same configuration

Modify the backup machine (192.168.16.129) keepalived configuration file

keepalived.conf:

​ #Detection script

vrrp_scriptchk_http_port{

Script"/usr/local/src/check_nginx_pid.sh"#Heartbeat execution script to detect whether nginx is started

interval2# (detection script execution interval)

weight2#weight

}

​#vrrp instance definition part

vrrp_instanceVI_1{

stateBACKUP#Specify the role of keepalived, MASTER is the main one and BACKUP is the backup one

interfaceens33#The current network interface card for vrrp communication (current centos network card) uses ifconfig to check your specific network card

​virtual_router_id66#Virtual routing number, the master and slave must always be

priority99#Priority, the larger the value, the higher the priority of obtaining and processing the request

advert_int1#Checking interval, the default is 1s (vrrp multicast cycle seconds)

​#Authorized access

Authentication{

auth_typePASS#Set the authentication type and password. MASTER and BACKUP must use the same password for normal communication

auth_pass1111

}

track_script{

chk_http_port# (call detection script)

}

virtual_ipaddress{

192.168.16.130#Define virtual ip (VIP), you can set more than one, one per line

}

}

Detection script:

​ #!/bin/bash

​#Detect whether nginx is started

A=`ps-Cnginx--no-header|wc-l`

​if[$A-eq0];then#If nginx is not started, start nginx

systemctlstartnginx#restart nginx

​if[`ps-Cnginx--no-header|wc-l`-eq0];then#nginx fails to restart, stop the keepalived service and perform VIP transfer

killallkeepalived

fi

fi

Script authorization: chmod775check_nginx_pid.sh

Note: The script must be authorized, otherwise it will not have permission to access. Here we have two servers executing, VIP (virtual_ipaddress:192.168.16.130), we access the service directly through VIP in the production environment.

Simulate nginx failure:

Modify the Nginx html page that the two servers access by default as a difference.

First, access 192.168.16.130 through VIP. The page displays 192.168.16.128; indicating that it is currently a service provided by the main server.

At this time, the 192.168.16.128 main server executes the command:

systemctlstopnginx;#stop nginx

When I visited VIP (192.168.16.130) again, I found that the page still displayed: 192.168.16.128. This was an automatic restart in the script.

Now directly close the 192.168.16.128 server, visit VIP here (192.168.16.130) and now find that the page shows 192.168.16.129. At this time, keepalived will automatically fail over, and a high-availability solution for an enterprise-level production environment has been established.

There are many functions in keepalived, such as email reminders, etc., but they are not available. You can go to the official website to read the documentation.

The above is the detailed content of Nginx high availability method. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

See all articles