Keepalived implements highly available Nginx reverse proxy

Source: Internet
Author: User
Tags nginx reverse proxy

Keepalived implements highly available Nginx reverse proxy

Experiment System:

(1) CentOS 6.6 _ x86_64;

(2) There are three hosts. In this tutorial, the host names are IP addresses: 131, 132, and 133.

Prerequisites: Firewall and selinux are both disabled and time synchronization between hosts

Experimental software: nginx-x86_64-1.6.2keepalived-x86_64-1.2.13

Tutorial topology:

1. install and configure keepalived

1. directly use yum for installation. 133 the host only installs nginx:

Yum-y install keepalived nginx

2. Configure keepalived on host 131:

Vim/etc/keepalived. conf
--------------------------------------------
! Configuration File for keepalived

Global_defs {// this segment is ignored
Notification_email {
Acassen@firewall.loc
Failover@firewall.loc
Sysadmin@firewall.loc
}
Notification_email_from Alexandre.Cassen@firewall.loc
Smtp_server 192.168.200.1
Smtp_connect_timeout 30
Router_id LVS_DEVEL
}

Vrrp_instance VI_1 {
State MASTER // defined as a MASTER host
Interface eth0
Virtual_router_id 55 // virtual id
Priority 100 // the priority.
Advert_int 1 // detection Interval
Authentication {
Auth_type PASS // authentication method
Auth_pass abcd // authentication Password
}
Virtual_ipaddress {
192.168.245.150 // virtual ip address, that is, VIP
}

Notify_master "/etc/init. d/nginx start" // The action executed after the MASTER node becomes
Notify_backup "/etc/init. d/nginx stop" // action executed after BACKUP
Notify_fault "/etc/init. d/nginx stop" // action executed after FAULT
}

3. Upload the configuration file to the 132 host and configure:

Vim/etc/keepalived. conf

-------------------------------------------------- Modify only the following two rows:

State BACKUP // defined as BACKUP

Priority 99 // priority

4. The keepalived service is started on two hosts. Currently, 131 is the MASTER host, so the services are started on this host:

Service keepalived start

Ip addr show

5. Stop the keepalived service of the 131 host. You can see that the service has been transferred to the 132 Host:

Service keepalived stop

6. Start the keepalived service of the 131 host again. It can be seen from the log that it becomes the MASTER again:

2. Configure nginx reverse proxy

1.131 and 132 host configuration nginx:

Vim/etc/nginx/conf. d/default. conf
----------------------------------------------
Location/{// Add this segment
Proxy_pass http: // 192.168.245.timeout;
}

# Location/{// comment out this segment
# Root/usr/share/nginx/html;
# Index index.html index.htm;
# Example
# ModSecurityEnabled on;
# ModSecurityConfig/etc/nginx/modsecurity. conf;
#}
----------------------------------------------
Service nginx restart
Service keepalived restart

2.133 host startup test page:

Rm-rf/usr/share/nginx/html/* // Delete the original page file
Vim/usr/share/nginx/html/index.html
---------------------------------------------
<H1> This is a test! </H1>
---------------------------------------------
Service nginx start

Access http: // 192.168.245.150 and page files on the 133 host are displayed, and reverse proxy is successful. Disable the keepalived service of the 131 host. If the 132 host changes to the MASTER, the page is still accessible. However, high availability cannot be achieved when the nginx process is killed.

Now, the experiment has been demonstrated. Thank you! If you have any questions, contact me.

Configure and optimize reverse proxy and load balancing in Nginx

RHEL6.4 set up Nginx Reverse Proxy Server

Thoughts on how to configure and set up Nginx reverse proxy

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Nginx details: click here
Nginx: click here

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.