一、nginx 编译安装
1.依赖环境安装
     yum -y install gcc gcc-c++ zlib zlib-devel pcre pcre-devel openssl openssl-devel
     yum -y install kernel-devel Popt popt-devel
     yum -y groupinstall "Development Tools" "Development Libraries"

2.编译安装
    [root@localhost src]# tar -xvf nginx-1.8.0.tar.gz
    [root@localhost nginx-1.8.0]#cd nginx-1.8.0

#--with-http_stub_status_module :可显示nginx连接数

[root@localhost nginx-1.8.0]# ./configure --with-http_stub_status_module --with-http_dav_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module
[root@localhost nginx-1.8.0]# make && make install

3.添加脚本
[root@localhost]# vim /etc/init.d/nginx
#---------------------------------------------------------------------------------------------------------------------------
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
start
}

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 "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

#---------------------------------------------------------------------------------------------------------------------------
[root@localhost]# chmod +x /etc/init.d/nginx

二、配置upstream 模块
配置nginx.conf配置文件
1.在http{}中添加一个upstream.conf配置文件
include /usr/local/nginx/conf/upstream.conf;
     upstream www_test {
          #ip_hash; #如要看到测轮循的效果,这里需注释掉
          server 192.168.0.116:80;
          server 192.168.0.40:80;
}

2.将http{}中的server模块单独提取出来
include /usr/local/nginx/vhost/*.conf;

配置test 服务 /usr/local/nginx/vhost/test.conf
     server {
          listen 80;
          server_name localhost;

location / {
          proxy_pass http://www_test;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

三、安装配置keeplived服务
1.安装
两台前端nginx 开启upstream的服务器安装keepalived服务

[root@localhost src]# tar -xvf keepalived-1.1.15.tar.gz
     [root@localhost src]# cd keepalived-1.1.15
     [root@localhost keepalived-1.1.15]# ./configure --prefix=/usr/local/keepalived
     [root@localhost keepalived-1.1.15]# make && make install

[root@localhost keepalived-1.1.15]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
     [root@localhost keepalived-1.1.15]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
     [root@localhost keepalived-1.1.15]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
     [root@localhost keepalived-1.1.15]# cp -r /usr/local/keepalived/etc/keepalived/ /etc/

[root@localhost keepalived-1.1.15]# /etc/init.d/keepalived start

2.修改配置文件
[root@localhost keepalived-1.1.15]# vim /etc/keepalived/keepalived.conf
global_defs {
    notification_email {
   localhost@qq.com
}
    notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id node1
}

vrrp_instance VI_1 {
     state MASTER #主设置MASTER 从设置为BACKUP
     interface eth0 #eth0 为自己的网卡,要通过ifconfig 查看己的网卡名
     virtual_router_id 51
     priority 100 #备机要小于主
     advert_int 1
     authentication {
     auth_type PASS
     auth_pass 1111
}
virtual_ipaddress {
     192.168.0.200 #虚拟IP
}
}

virtual_server 192.168.0.200 80 {
     delay_loop 6
     lb_algo rr
     lb_kind DR
     nat_mask 255.255.255.0
     persistence_timeout 50
     protocol TCP

real_server 192.168.0.116 80 {
weight 1
TCP_CHECK {
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3
      connect_port 80
   }
}
real_server 192.168.0.40 80 {
weight 1
TCP_CHECK {
     connect_timeout 3
     nb_get_retry 3
     delay_before_retry 3
     connect_port 80
}
}
}

[root@localhost ]# /etc/init.d/keepalived start    #keepalived 服务启动命令

四、后端可拿两台web 服务器来做测试即可

nginx+keeplived负载均衡配置的更多相关文章

  1. nginx四层负载均衡配置

    nginx四层负载均衡配置代理Mysql集群 环境如下: ip 192.168.6.203 Nginx ip 192.168.6.*(多台) Mysql 步骤一 查看Nginx是否安装stream模块 ...

  2. Nginx安装负载均衡配置 fair check扩展

    前言 本文主要是针对Nginx安装.负载均衡配置,以及fair智能选举.check后端节点检查扩展功能如何扩展,进行讲解说明. fair模块: upstream-fair,“公平的”Nginx 负载均 ...

  3. Nginx + Tomcat 负载均衡配置详解

    Nginx常用操作指南一.Nginx 与 Tomcat 安装.配置及优化1. 检查和安装依赖项 yum -y install gcc pcre pcre-devel zlib zlib-devel o ...

  4. Nginx+tomcat负载均衡配置

    Nginx+tomcat是目前主流的java web架构,如何让nginx+tomcat同时工作呢,也可以说如何使用nginx来反向代理tomcat后端均衡呢?直接安装配置如下: 1.JAVA JDK ...

  5. CentOS6.5安装nginx及负载均衡配置

    所有的安装包可以去以下地址下载,或者自行去官网下载,下面都有介绍. 所有安装包地址:http://download.csdn.net/detail/carboncomputer/9238037 原文地 ...

  6. nginx的负载均衡配置,常用策略

    场景:nginx是一款非常优秀的负载均衡服务器,小巧而且性能强悍,中小型企业的首选. 下面介绍nginx的负载均衡的几种常见的配置以及优缺点 第一种:轮询(默认) 优点:实现简单 缺点:不考虑每台服务 ...

  7. centos6 Nginx+Tomcat负载均衡配置

    一.Nginx简介 Nginx是一个web服务器也可以用来做负载均衡及反向代理使用,目前使用最多的就是负载均衡,具体简介我就不介绍了百度一下有很多,下面直接进入安装步骤 二.Nginx安装 1.下载N ...

  8. Nginx的负载均衡配置(七)

    原文链接:https://www.cnblogs.com/knowledgesea/p/5199046.html 首先给大家说下upstream这个配置的,这个配置是写一组被代理的服务器地址,然后配置 ...

  9. Linux记录-Nginx+Tomcat负载均衡配置

    Nginx负载均衡配置及策略: 轮询(默认) 优点:实现简单缺点:不考虑每台服务器的处理能力配置示例如下:upstream www.xxx.com {# 需要负载的server列表server www ...

随机推荐

  1. 突破IP限制登入网站后台

    后台: http://www.zhuna.cn/sys_admin/ 输入账号密码 点击确定后,跳转到登录界面: http://www.zhuna.cn/sys_admin/login.php 很自然 ...

  2. 在 WF 4 中编写自定义控制流活动

    在 WF 4 中编写自定义控制流活动 Leon Welicki 控制流是指组织和执行程序中各个指令的方法. 在 Windows Workflow Foundation 4 (WF 4) 中,控制流活动 ...

  3. DOS命令:列出某目录下的所有文本文件名并重定向到某文件

    命令如下: >dir /b *.txt>output.txt dir无需说,/b 是只要文件名,>是重定向. 2013年11月7日13:36:57

  4. 无需编码开发快速设计互动式UI - uilang

    uilang是一个非常小巧的ui类库,可以帮助不熟悉前端代码的web设计人员快速的开发互动式UI.你只需要使用“语义式”的说明来控制元素的动态效果. 开发中你只需要在<code>标签内部输 ...

  5. Unity3D开发之Mac OS 开发环境搭建 笔记

    http://www.cnblogs.com/zhaoqingqing/p/3383167.html 首先上几张图: 摸索了一上午,才搞定在模拟器中运行.至于在Iphone真机中运行,虽然有开发者证书 ...

  6. filter中的dispatcher解析

    两种include方式 我自己写了一个original.jsp,另外有一个includedPage.jsp,我想在original.jsp中把includedPage.jsp引进来有两种方式: 1.& ...

  7. ant design pro (十一)advanced Mock 和联调

    一.概述 原文地址:https://pro.ant.design/docs/mock-api-cn Mock 数据是前端开发过程中必不可少的一环,是分离前后端开发的关键链路.通过预先跟服务器端约定好的 ...

  8. 有关WINDOWS XP登录密码的几种方法

    网上关于破解WINDOWS XP登录密码的几种方法很多,但许多根本不行.我现在总结自己试过几种,这几种方法其实均比较菜的,但可行的.WINDOWS XP登录是由SAM 文件管理的,总体来说“山姆大叔” ...

  9. Android studio中导入第三方类库

    常常在github上看到一些好用的框架,但是对于一个新手怎样在android studio上导入去总会遇到各种麻烦,索性来研究下第三方类库怎样在android studio上导入. 以我在github ...

  10. mysql 禁制远程连接

    改表法:把host地址改为% use mysql; update user set host = '%' where user = 'root'and host="127.0.0.1&quo ...