How to Implement High Availability of Nginx processes in Nginx + Keepalived

Source: Internet
Author: User

I will briefly describe this architecture:
Generally, for the convenience of maintenance, the servers of the enterprise website are all in their internal data centers, and only two ports 80 and 443 of the Keepalived VIP address are opened, which are mapped out through the Juniper SSG550 firewall, the Internet DNS corresponds to the mapped public IP address. The firewall and network security of this architecture are described as follows: This system architecture only maps ports 80 and 443 of the Intranet VIP to the Juniper SSG550 firewall of the Internet, and other ports are disabled, all Intranet machines disable iptables and ipfw firewalls. The Internet DNS directs to the Internet address mapped by Juniper or USG5000. This section is based on my project plan. This load balancing method is also applied to our company's e-commerce website. It has been online for more than a year. Through the following content, you can quickly build an enterprise-level Server Load balancer high-availability Web environment. In terms of Server Load balancer High Availability Technology, I have always promoted the high availability architecture of Server Load balancer using Nginx + Keepalived as Web, and actively applied it to real projects, this architecture is ideal for flexible and stable environments. Nginx Server Load balancer generally suffers from network faults such as loose server network cables, crash due to damage to server hardware, and the death of Nginx service process. Theoretically, but in fact, I have never encountered this situation on the online server, which is enough to prove the stability of Nginx as the Server Load balancer/reverse proxy server. We can solve this problem through technical means );

The implementation steps are as follows:
1. install and configure Nginx and Keepalived. You can refer to the articles in my topic series at http://network.51cto.com/art/201007/209823.htm.

2. Write SHELL scripts to implement high HA of Nginx service processes. The script content is as follows:

#! /Bin/bash
While:
Do
Nginxpid = 'ps-C nginx -- no-header | wc-l'
If [$ nginxpid-eq 0]; then
/Usr/local/nginx/sbin/nginx
Sleep 5
Nginxpid = 'ps-C nginx -- no-header | wc-l'
Echo $ nginxpid
If [$ nginxpid-eq 0]; then
/Etc/init. d/keepalived stop
Fi
Fi
Sleep 5
Done

I would like to explain that this is an infinite loop script, which is placed on the primary Nginx machine because it is mainly used to provide services) and executed every 5 seconds, use the ps-C command to collect whether the nginx PID value is 0. If it is 0, the Nginx process is dead.) try to start the nginx process. If it continues to be 0, that is, if nginx fails to be started, the Keeplaived process of the local machine will be disabled, and the VIP address will be taken over by the standby machine. Of course, the entire website will be provided by the Nginx of the standby machine, this ensures the high availability of the Nginx process.

Of course, there are other methods. For example, we can write another SHELL script, the index. php or index. jsp file of the wget homepage every five seconds or ping the VIP address of the website. If $? The return code is a non-zero value, that is, an error). We can also disable the primary Nginx machine and take over from the standby machine. If you are interested, you can write these scripts on your own.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.