First, the environment configuration:
ip:192.168.1.30 vip:192.168.1.34 Keepalive+nginx
ip:192.168.1.31 vip:192.168.1.34 Keepalive+nginx
ip:192.168.1.35 Web1
ip:192.168.1.36 WEB2
Second, two load-balanced machines are installed Keepalived+nginx
1, installation keepalived
# yum-y Install keepalived
2. Create Nginx user group and Nginx user
# Groupadd-g nginx-r nginx
3. Compile and install nginx-1.4.7
# Tar XF nginx-1.4.7. tar.gz# CD Nginx-1.4.7# ./configure--prefix=/usr--sbin-path=/usr/sbin/nginx--conf-path=/etc/nginx/nginx.conf--error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log--pid-path=/var/run/nginx/nginx.pid--Lock-path=/var/Lock/nginx.Lock--user=nginx--group=nginx--with-http_ssl_module--with-http_flv_module--with-http_stub_status_module--with-http _gzip_static_module--http-client-body-temp-path=/usr/local/nginx/client/--http-proxy-temp-path=/usr/local/ nginx/proxy/--http-fastcgi-temp-path=/usr/local/nginx/fcgi/--http-uwsgi-temp-path=/usr/local/nginx/uwsgi-- http-scgi-temp-path=/usr/local/nginx/scgi--with-pcre
is-y install gcc gcc-c++ autoconf automake make appears. /-y install pcre-devel appears. /-y install OpenSSL openssl-devel
# Make && make install
4. Check the configuration file for syntax errors
# Nginx-is"/usr/local/nginx/client/" failed (2 /etc/nginx/nginx.conf Test Failed
5, modify the error, re-detection
# mkdir/usr/local/- is is successful
6. Provide SYSV init script for Nginx
# vim/etc/rc.d/init.d/nginx#!/bin/sh## Nginx- Thisscript starts and stops the Nginx daemon## chkconfig:- - the# Description:nginx isAn http (s) server, HTTP (s) reverse # Proxy and IMAP/POP3 proxy server# processname:nginx# config:/etc/nginx/nginx.conf# config:/etc/sysconfig/nginx# pidfile:/var/run/Nginx.pid # Source function library:/etc/rc.d/init.d/functions # Source Networking configuration:/etc/sysconfig/Network # Check that networking isUp . [ "$NETWORKING"="No"] && exit0Nginx="/usr/sbin/nginx"Prog=$ (basename $nginx) Nginx_conf_file="/etc/nginx/nginx.conf" [ -f/etc/sysconfig/nginx] &&. /etc/sysconfig/Nginx Lockfile=/var/Lock/subsys/Nginx Make_dirs () {# Make required directories user= ' Nginx-v2>&1| Grep"Configure arguments:"| Sed's/[^*]*--user=\ ([^]*\). */\1/g'-' Options= ' $nginx-V2>&1| Grep'Configure arguments:'` forOptinch$options; Do if[' Echo $opt | grep'. *-temp-path'` ]; Then value= ' echo $opt | Cut-d"="-F2` if[!-D"$value" ]; Then # echo"Creating"$value mkdir-P $value && chown-R $user $value fi fi Done} start () {[-X $nginx] | | Exit5 [ -F $NGINX _conf_file] | | Exit6Make_dirs Echo-N $"starting $prog:"Daemon $nginx-c $NGINX _conf_file retval=$?Echo [$retval-eq0] &&Touch $lockfilereturn$retval} stop () {echo-N $"stopping $prog:"Killproc $prog-QUIT retval=$?Echo [$retval-eq0] && RM-F $lockfilereturn$retval} restart () {configtest||return$?Stop Sleep1start} reload () {configtest||return$?Echo-N $"Reloading $prog:"Killproc $nginx-HUP RETVAL=$?echo} force_reload () {restart} configtest () {$nginx-T-c $NGINX _conf_file} rh_status () {Status $prog} rh_status_q () {rh_status>/dev/NULL 2>&1} Case " $" inchstart) Rh_status_q&& exit0 $1 ;; Stop) Rh_status_q|| Exit0 $1 ;; Restart|configtest) $1 ;; Reload) Rh_status_q|| Exit7 $1 ;; Force-reload) force_reload;; status) Rh_status;; Condrestart|Try-restart) rh_status_q|| Exit0 ;; *) echo $"Usage: $ {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"Exit2Esac
8, give Nginx script execution permissions, add to the service management list, and let it boot automatically
# chmod +x/etc/rc.d/init.d/--add nginx# chkconfig nginx on
Third, configure Nginx as the reverse proxy server
1. Edit the nginx.conf configuration file
# cd/etc/nginx/# Vim nginx.conf #在nginx. Add the following content to the HTTP segment in the Conf configuration file upstream webservers {192.168. 1.35; 192.168. 1.36 ;} server { a; server_name www.hhh.com; / { proxy_pass http://webservers; Proxy_set_header x-real-IP $remote _addr; }}
Note: The contents of the nginx.conf of the two load hosts are the same
Iv. Configuration keepalived
1. Edit the keepalived.conf configuration file
# cd/etc/keepalived/
# Vim Keepalived.conf
The contents of the configuration file for #IP地址为192.168.1.30 are:
! Configuration File for Keepalived
Global_defs {
Notification_email {
[Email protected]
[Email protected]
[Email protected]
}
Notification_email_from [email protected]
Smtp_server 192.168.200.1
Smtp_connect_timeout 30
router_id Lvs_devel
}
Vrrp_instance Vi_1 {
State MASTER
Interface eth0
VIRTUAL_ROUTER_ID 51
Priority 100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {
192.168.1.34
}
}
Notify_master "/etc/init.d/nginx Start"
Notify_backup "/etc/init.d/nginx Stop"
Notify_fault "/etc/init.d/nginx Stop"
}
#IP地址为192.168.1the contents of the. 31 configuration file are:! Configuration File forKeepalivedglobal_defs {notification_email {[email protected] [email protected] [email protected]} Notification_email_from [email protected]! Configuration File forKeepalivedglobal_defs {notification_email {[email protected] [email protected] [email protected]} Notification_email_from [email protected] smtp_server192.168.200.1Smtp_connect_timeout -router_id lvs_devel}vrrp_instance vi_1 {State BACKUPInterfaceeth0 virtual_router_idWuyi Priority AboutAdvert_int1Authentication {auth_type PASS auth_pass1111} virtual_ipaddress {192.168.1.34}} notify_master"/etc/init.d/nginx Start"Notify_backup"/etc/init.d/nginx Stop"Notify_fault"/etc/init.d/nginx Stop"}
2. Configuring Web1 and WEB2 Servers
#yum Install Httpd-y
3, Edit web1 test page, start httpd service
# Vim/var/www/html/index.html content:
#/ETC/INIT.D/HTTPD Start
4, Edit web2 test page, start httpd service
# Vim/var/www/html/index.html content:/etc/init.d/httpd Start
V. Start the keepalived service and test
1, start the keepalived service of two hosts
#/etc/init.d/keepalived Start
2. Enter 192.168.1.34 refresh in the browser to get the following results
3, close 192.168.1.30 keepalived Service
#/etc/init.d/keepalived Stop
4, again in the browser input 192.168.1.34
5, by the above results can be, keepalived realized the Nginx reverse proxy high availability and nginx to achieve the Web load balancing effect
Keepalived implementation of high-availability Nginx reverse proxy