当前位置: 首页 > news >正文

nginx实现负载均衡

为什么80%的码农都做不了架构师?>>>   hot3.png

测试模型如下:

1、一台Nginx做负载负载均衡代理   具体配置centos5.8 ip 10.0.211.5

2、三台tomcat做业务逻辑处理        具体配置centos5.8 10.0.211.2 10.0.211.3 10.0.211.4

具体部署步骤:

1、          安装正则表达式支持库nginx支持rewrite重写

[root@server soft]# tar  xzvf  pcre-7.8.tar.gz

#解压安装包

[root@server pcre-7.9]# cd pcre-7.9

#进入解压后的安装包

[root@server pcre-7.9]# ./configure

#配置安装环境

[root@server pcre-7.9]# make && make install

#编译安装

2、          安装nginx

[root@server soft]# tar -zxvf nginx-1.3.3.tar.gz

#解压安装包

[root@server soft]# cd nginx-1.3.3

[root@server nginx-1.3.3]#  ./configure  --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

#配置编译环境

[root@server nginx-1.3.3]# make && make install

#编译安装

3、          配置Nginx

详细配置参数如下所示:

user www        www;

#启动用户

#user  nobody;

worker_processes  48;

#进程数量

error_log  logs/error.log crit;

#日志路径

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

pid        logs/nginx.pid;

#启动pid存储文件

worker_rlimit_nofile 65536;

#允许用户打开的最大句柄

events {

use epoll;

#查询模式

worker_connections  65536;

#最大句柄,允许连接的最大文件数量

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    charset  UTF-8;

server_names_hash_bucket_size 128;

#根据cpu一级缓存设置 ubuntu可以使用lscpu查看

    client_header_buffer_size 4;

    large_client_header_buffers 4 8k;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

sendfile        on;

#指定nginx是否调用sendfile函数(zero copy方式)来输出文件,普通应用必须设为on,对于普通文件用on。如果进行下载I/O负载应用,设置为off,以平衡磁盘磁盘和网络I/O处理速度。

#tcp_nopush     on;

#允许或禁止使用socketTCP_NOPUSH仅当sendfile on时设置改选项生效

keepalive_timeout  65;

#超时时间

    tcp_nodelay on;

    fastcgi_connect_timeout 300;

    fastcgi_send_timeout 300;

    fastcgi_read_timeout 300;

    fastcgi_buffer_size 64k;

    fastcgi_buffers 4 64k;

    fastcgi_busy_buffers_size 128k;

    fastcgi_temp_file_write_size 128k;

    #keepalive_timeout  0;

gzip  on;

#允许压缩传输文件

gzip_min_length 1k;

#设置允许压缩最小字节数,0表示多大都压,最好设置大于1k,小于1k越压越大

    gzip_buffers 4 16k;

gzip_http_version 1.1;

#判断http协议版本,是否支持压缩,否则用户看到乱码。默认即可

gzip_comp_level 2;

#gzip压缩比,压缩比最小处理速度最快,压缩比最大但处理最慢

    gzip_types text/plain application/x-javascript text/css application/xml;

gzip_vary on;

#加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩

client_max_body_size 50m;

#允许客户端请求的最大单个文件字节数

client_body_buffer_size 128k;

#缓冲区代理用户端请求的最大字节数,可以理解先保存到本地在发送给用户

proxy_connect_timeout 600;

#后端服务器连接的超时时间,发起握手等候相应超时时间

proxy_read_timeout 600;

连接成功后,等待后端服务器相应时间,已经进入后端的排队之中等候处理

proxy_send_timeout 600;

#后端服务器数据回传时间,在规定时间内服务器必须传完所有数据

proxy_buffer_size 16k;

#保存用户头信息供nginx进行规则处理,用于缓存代理请求

proxy_buffers    4 32k;

#nginx保存每个用户Buffer最大空间

proxy_busy_buffers_size 64k;

#如果系统很忙可以申请更大的proxy_buffers,官方推荐 *2

    proxy_temp_file_write_size 64k;

upstream fengzhanhai{

#负载均衡池

    #    ip_hash;

        server 10.0.211.2 down weight=4 max_fails=2 fail_timeout=30s;

        server 10.0.211.2:8080 weight=4 max_fails=2 fail_timeout=30s;

       # server 10.0.211.3:8080 weight=4 max_fails=2 fail_timeout=30s;

       # server 10.0.211.4:8080 weight=4 max_fails=2 fail_timeout=30s;

       # server 192.168.202.81:8080 weight=2 max_fails=2 fail_timeout=30s;

        server 192.168.202.82 weight=4  max_fails=2 fail_timeout=30s;

   }

server {

#虚拟主机反向代理负载均衡池fengzhanhai的主机应用

        listen       80;

        server_name  www.123.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

#当分发的后端业务宕机后无缝切换到pool中存活的业务之上

            proxy_pass http://fengzhanhai;

#反向代理资源池

            proxy_set_header Host www.123.com;

            proxy_set_header X-Forwarded-For  $remote_addr;

            root   html;

            index  index.html index.htm;

        }

        access_log  logs/feng.log  combined;

#访问日志

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    }

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

    # HTTPS server

    #

    #server {

    #    listen       443;

    #    server_name  localhost;

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers   on;

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

}

4、          测试Nginx配置语法是否正常

5、          ./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

6、          启动nginx进行测试./nginx

7、          Nginx负载均衡知识拓展

proxy_set_header  Host  $host :首先说明 proxy_set_header 指令在向反向代理的后端Web服务器发起请求时添加指定的 Header头信息,后端web服务器有多个基于域名的虚拟主机时,通过头信息Host,用于指定请求的域名,这样后端web才能识别反向代理请求哪个虚拟主机处理。

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for :在nginx反向代理添加Header头信息 X-Forwarded-For在配合后端服务器日志文件的$http_x_Forwarded_for这条就可以获得用户的IP地址

8、          nginx支持的几种算法分析

1 轮询 每个请求按时间顺序分配到不同的后端服务器了,后端服务器down掉,自动切除;

2weight  设定服务器权值: weight=2    服务器性能不均时候使用 weight 默认为1weight越大,负载的权重越大;

3 ip_hash 每个请求按访问iphash结果分配,每个访客有固定的后端服务器,可以解决session问题;

4 fair(第三方) 按后端服务器的响应时间来分配,响应时间短的优先分配

5url_hash (第三方按访问的urlhash结果分配,使每个url定向到同一个后端服务器,后端为缓存服务器比较有效。

详细参数介绍:

1down  当前的IP server暂时不参与负载,不进行反向代理;

2max_fails 允许请求失败的次数默认为1,当超过最大次数时,返回proxy_next_upstream模块定义的错误;

3fail_timeout  max_fails次失败后,暂停的时间;

4backup  其它所有非backup机器down或者忙时候,请求backup机器,这台机器压力最轻。

转载于:https://my.oschina.net/liting/blog/418132

相关文章:

  • Makefile文件的编写(1)
  • CDH使用之CM、CDH4、5卸载
  • 小菜学设计模式——单一职责原则
  • 算法-最大公约数
  • Androd开发之广告栏设计
  • nginx+lua+redis(openresty)配置
  • 模拟 2015百度之星资格赛 1003 IP聚合
  • 增值税发票管理解决方案
  • SQL Server利用RowNumber()内置函数与Over关键字实现通用分页存储过程(支持单表或多表结查集分页)...
  • Ubuntu 下 Mysql 新建数据库和用户
  • 运维角度浅谈MySQL数据库优化
  • springmvc常用的组件,注解,跳转
  • Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造
  • ZH奶酪:【数据结构与算法】并查集基础
  • lnmp 在nginx中配置相应的错误页面error_page
  • Google 是如何开发 Web 框架的
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • co.js - 让异步代码同步化
  • EventListener原理
  • FineReport中如何实现自动滚屏效果
  • JAVA并发编程--1.基础概念
  • learning koa2.x
  • Spring Cloud中负载均衡器概览
  • Terraform入门 - 1. 安装Terraform
  • Terraform入门 - 3. 变更基础设施
  • Vue.js 移动端适配之 vw 解决方案
  • vue+element后台管理系统,从后端获取路由表,并正常渲染
  • Web Storage相关
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 工作手记之html2canvas使用概述
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 开源SQL-on-Hadoop系统一览
  • 盘点那些不知名却常用的 Git 操作
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 深度学习中的信息论知识详解
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 延迟脚本的方式
  • 智能合约开发环境搭建及Hello World合约
  • ​520就是要宠粉,你的心头书我买单
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • #、%和$符号在OGNL表达式中经常出现
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (一)Java算法:二分查找
  • .Net Core 中间件验签
  • [ 代码审计篇 ] 代码审计案例详解(一) SQL注入代码审计案例
  • [51nod1610]路径计数
  • [BeginCTF]真龙之力
  • [C#]猫叫人醒老鼠跑 C#的委托及事件
  • [CISCN2019 华东南赛区]Web11
  • [CSS] - 修正IE6不支持position:fixed的bug
  • [CTO札记]如何测试用户接受度?
  • [Docker]十.Docker Swarm讲解