实战:

一、源码编译安装nginx

[root@tiandong63 ~]#yum groupinstall "Development Tools" "Development Libraries" -y          安装开发包
[root@tiandong63 ~]#yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* -y     安装依赖包

[root@tiandong63 ~]#tar -zxvf nginx-1.9.4.tar.gz -C /usr/local/src/
[root@tiandong63 ~]#cd /usr/local/src/
[root@tiandong63 src]#cd nginx-1.9.4/
[root@tiandong63 nginx-1.9.4]#./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module

参数:
--with-http_dav_module         启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启
--with-http_stub_status_module 启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
--with-http_addition_module    启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
--with-http_sub_module         启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
--with-http_flv_module         启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)

--with-http_mp4_module         启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件
 [root@tiandong63 nginx-1.9.4]#make && make install
 [root@tiandong63 nginx-1.9.4]#useradd -M -u 8001 -s /sbin/nologin nginx
[root@tiandong63 ~]# /usr/local/nginx/sbin/nginx     启动nginx
[root@tiandong63 ~]# netstat -antup|grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      42350/nginx
测试nginx

二、nginx服务器日常操作:

配置nginx为分发器实现动静分离

vim /usr/local/nginx/conf/nginx.conf

2 user  nginx nginx;
43         location / {
 44             root   html;
 45             index  index.html index.htm;
 46         if ($request_uri ~* \.html$){
 47                    proxy_pass http://htmlservers;
 48            }
 49         if ($request_uri ~* \.php$){
 50                    proxy_pass http://phpservers;
 51            }
 52                    proxy_pass http://picservers;
 53         }
123 upstream  htmlservers {   #定义负载均衡服务器组名称
124          server 192.168.199.4:80;
125          server 192.168.199.5:80;
126  }
127  upstream  phpservers{
128          server 192.168.199.4:80;
129          server 192.168.199.5:80;
130  }
131  upstream  picservers {
132          server 192.168.199.4:80;
133          server 192.168.199.5:80;
134  }
135 }

把一下文件注释了,否组php文件直接在nginx服务器上解析了,不在解析给后端的服务器。


[root@tiandong63 ~]# ln -s /usr/local/nginx/sbin/* /usr/bin/
[root@tiandong63 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@tiandong63 ~]# nginx -s reload
三、配置后端服务器(tiandong64和tiandong65上面配置一样):
[root@tiandong64 ~]# yum install php httpd -y
[root@tiandong65 ~]# yum -y install php httpd -y
[root@tiandong64 ~]# cd /var/www/html/
[root@tiandong64 html]# more index.html
192.168.199.4
[root@tiandong64 html]# more test.php
192.168.199.4
<?php
    phpinfo();
?>
[root@tiandong64 html]#
[root@tiandong64 html]# ll
total 12
-rw-r--r-- 1 root root  14 Mar 20 00:24 index.html
-rw-r--r-- 1 root root 790 Dec 24  2018 pic.PNG
-rw-r--r-- 1 root root  35 Mar 20 00:25 test.php
重启Apache服务:
[root@tiandong64 ~]# /etc/init.d/httpd restart
[root@tiandong65 ~]# /etc/init.d/httpd restart
四、测试:

测试html页面:

[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
测试php页面:

测试服务器性能:

[root@tiandong66 ~]# ab -n 1000 -c 1000 http://192.168.199.3/index.html   运行正常
[root@tiandong66 ~]# ab -n 2000 -c 2000 http://192.168.199.3/index.html   运行报错
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.199.3 (be patient)
socket: Too many open files (24)     #测试时,一次打开的socket文件太多。系统默认的一次打开的最多问1024个
[root@tiandong66 ~]# ulimit -n 10240 
[root@tiandong63 ~]# ulimit -n 10240

五、nginx负载的五中策略设置方法:
1、轮询(默认)
upstream  htmlservers {   #定义负载均衡服务器组名称
         server 192.168.199.4:80;
         server 192.168.199.5:80;
 }
2、指定权重(指定轮询几率,weight和访问比例成正比,用户后端服务器性能不均的情况)
upstream  htmlservers {   #定义负载均衡服务器组名称
         server 192.168.199.4:80 weight=1;
         server 192.168.199.5:80 weight=2;
 }
3、IP绑定 ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream backserver {
ip_hash;
server 192.168.199.4:80;
server 192.168.199.5:80;
}
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backserver {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
六、故障演练
当其中一台Apache宕机

[root@tiandong64 ~]# /etc/init.d/httpd stop
Stopping httpd:                                            [  OK  ]

[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
一直访问的是另外一台机器

故障恢复:

[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
OK!!!!

nginx实现动静分离的负载均衡集群的更多相关文章

  1. 使用nginx实现动静分离的负载均衡集群

    一.概述: LB负载均衡集群分两类: LVS (四层)和 nginx或haproxy (七层) 客户端通过访问分发器的VIP来访问网站 |现在应用更复杂,比如现在网站页面有: .php .html . ...

  2. Nginx + Tomcat 动静分离实现负载均衡(转)

    0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 # 定义Nginx运行的用户 和 用户组 如果对 ...

  3. Nginx + Tomcat 动静分离实现负载均衡

    0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 # 定义Nginx运行的用户 和 用户组 如果对 ...

  4. 【转载】Nginx+Tomcat 动静分离实现负载均衡

    0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 1 # 定义Nginx运行的用户 和 用户组 如 ...

  5. 负载均衡 | Nginx+Tomcat 动静分离实现负载均衡

    0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 基本配置这个文件,就可以实现负载了.但是里面的各 ...

  6. Nginx+Keepalived搭建高可用负载均衡集群

    本文的重点是Keepalived的配置,Nginx的配置就简略带过.软件:CentOS 7.2 / Nginx 1.12.2 / Keepalived 1.3.9 ha-01:192.168.1.97 ...

  7. Nginx 动静分离与负载均衡的实现

    一.前提 企业中,随着用户的增长,数据量也几乎成几何增长,数据越来越大,随之也就出现了各种应用的瓶颈问题. 问题出现了,我们就得想办法解决,一般网站环境,均会使用LAMP或者LNMP,而我们对于网站环 ...

  8. Keepalived+Nginx实现高可用负载均衡集群

    一 环境介绍 1.操作系统CentOS Linux release 7.2.1511 (Core) 2.服务keepalived+nginx双主高可用负载均衡集群及LAMP应用keepalived-1 ...

  9. Apache和Nginx负载均衡集群及测试分析

    一.应用场景介绍 本文主要是介绍Apache和Tomcat在Linux环境下的安装讲解以及AJP协议动静分离负载均衡的实现,以及与Nginx负载性能比较.联网安装较为简单,故此处只说脱机的Linux环 ...

随机推荐

  1. Java写学生管理系统

    package Homework08;/*调试了一上午,收获:学会了昨天的debug的使用吸取教训:Student stus[]=new Student[2]; for (int i=0;i<s ...

  2. 使用Enablebuffering多次读取Asp Net Core 3.0 请求体 读取Request.Body流

    原文:使用Enablebuffering多次读取Asp Net Core 请求体 使用Enablebuffering多次读取Asp Net Core 请求体 1 .Net Core 2.X时代 使用E ...

  3. thinkphp 4.8 漏洞测试

    首先要部署环境 这里利用docker的方便部署性,来直接找个现成的    git clone https://github.com/vulnspy/thinkphp-5.1.29.git 下载安装后, ...

  4. Django rest-framework框架-解析器

    解析器: 开始: django: request.POST/ request.body 满足一下两个要求POST中才有值 1. 如果请求头中的 Content-Type: application/x- ...

  5. Java基础第一天--继承、修饰符

    继承 继承的概述: 继承是面向对象三大特征之一.可以使得子类具有父类的属性和方法,还可以在子类中重新定义,追加属性和方法. //创建父类 public class Fu{ public void sh ...

  6. React中,input外边如果包一个div,可以把input的onChange事件绑定到div上面,并且也生效

    最近第一次开始学习封装组件,遇到几个比较神奇的问题. 首先就是如果input外边包一个div,如果把input的onChange事件绑定到div上,也会生效 <div onChange={(e) ...

  7. 安卓开发之生成XML文件

    package com.lidaochen.phonecall; import android.net.Uri; import android.os.Environment; import andro ...

  8. 7.JSP技术

    JSP和Servlet一样,都是用于开发动态web资源的技术 JSP的两个特性:1.写JSP代码就像是在写HTML代码,但JSP技术允许在页面中编写java代码(理解为一种特殊的HTML) 2.JSP ...

  9. ssh: Bad configuration option: usedns

    某天突然听到同事说服务器上git用不了了,上去一看,确实用不了了,git pull报出了如下错误: $ git pull /etc/: Bad configuration option: usedns ...

  10. mysql详解常用命令操作,利用SQL语句创建数据表—增删改查

    关系型数据库的核心内容是 关系 即 二维表 MYSQL的启动和连接show variables; [所有的变量] 1服务端启动 查看服务状态 sudo /etc/init.d/mysql status ...