1.集群的特点

(1)高性能

(2)价格有效性

(3)可伸缩性

(4)高可用性

(5)透明性

2.负载均衡概述

负载均衡,英文名称为Load Balance,其含义就是指将负载(工作任务)进行平衡、分摊到多个操作单元上进行运行,例如FTP服务器、Web服务器、企业核心应用服务器和其它主要任务服务器等,从而协同完成工作任务。

负载均衡构建在原有网络结构之上,它提供了一种透明且廉价有效的方法扩展服务器和网络设备的带宽、加强网络数据处理能力、增加吞吐量、提高网络的可用性和灵活性。

3.构建集群环境

三台web服务器配置一模一样

3.1web01
  1. vim /etc/nginx/conf.d/proxy.conf

  2. server{
  3. listen 80;
  4. server_name www.shuai.com;
  5.  
  6. location / {

  7. root /www/;
  8. index index.html;
  9. }
  10. }
3.2web02
  1. vim /etc/nginx/conf.d/proxy.conf

  2. server{
  3. listen 80;
  4. server_name www.shuai.com;
  5.  
  6. location / {

  7. root /www/;
  8. index index.html;
  9. }
  10. }
3.3web03
  1. vim /etc/nginx/conf.d/proxy.conf

  2. server{
  3. listen 80;
  4. server_name www.shuai.com;
  5.  
  6. location / {

  7. root /www/;
  8. index index.html;
  9. }
  10. }
3.4测试

4.负载均衡环境搭建

  1. vim /etc/nginx/conf.d/proxy.conf

  2. upstream shuai {
  3. server 10.0.0.7:80;
  4. server 10.0.0.8:80;
  5. server 10.0.0.9:80;
  6. }

  7. server {

  8. listen 80;
  9. server_name www.shuai.com;

  10. location / {

  11. proxy_pass http://shuai;
  12. proxy_set_header HOST $host;


  13. }

  14. }

4.1负载均衡模块详细说明
ngx_http_upstream_module

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

4.1.1轮训分配请求(平均)

默认配置

4.1.2weight=number权重分配请求
  1. upstream oldboy {
  2. server 10.0.0.7:80 weight=3;
  3. server 10.0.0.8:80 weight=2;
  4. server 10.0.0.9:80 weight=1;
  5. }

4.1.3backup`实现热备功能(将服务器标记为备份服务器。当主服务器不可用时,将传递请求。)

  1. upstream oldboy {
  2. server 10.0.0.7:80;
  3. server 10.0.0.8:80;
  4. server 10.0.0.9:80 backup;
  5. }

4.1.4max_fails=number定义最大失败次数(用于监控检测)

  1. upstream oldboy {
  2. server 10.0.0.7:80 max_fails=5;
  3. server 10.0.0.8:80 max_fails=5;
  4. server 10.0.0.9:80 max_fails=5;
  5. }

4.1.5fail_timeout=time定义失败之后重发的间隔时间(用于监控检测)

  1. upstream oldboy {
  2. server 10.0.0.7:80 fail_timeout=10s;
  3. server 10.0.0.8:80 fail_timeout=10s;
  4. server 10.0.0.9:80 fail_timeout=10s;
  5. }
4.2反向代理模块详细说明

更过模块参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

ngx_http_proxy_module

4.2.1proxy_set_heard负载均衡到后端web服务器无法显示页面

  1. location / {
  2.  
  3. proxy_pass http://shuai;
  4. proxy_set_header HOST $host;
  5.  
  6. }

4.2.2在access日志中显示源IP,而不是负载均衡IP

  1. location / {
  2.  
  3. proxy_pass http://shuai;
  4. proxy_set_header HOST $host;
  5. proxy_set_header X-Forwarded-For $remote_addr;
  6. proxy_next_upstream error timeout http_404 http_502;
  7. }

4.2.3proxy_next_upstream如果后端web出现错误页面,则跳过访问,将正确的页面显示给客户端。

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream

  1. location / {
  2.  
  3. proxy_pass http://shuai;
  4. proxy_set_header HOST $host;
  5. proxy_set_header X-Forwarded-For $remote_addr;
  6. proxy_next_upstream error timeout http_404 http_502;
  7. }

nginx负载均衡与反向代理的更多相关文章

  1. 【架构师之路】Nginx负载均衡与反向代理—《亿级流量网站架构核心技术》

    本篇摘自<亿级流量网站架构核心技术>第二章 Nginx负载均衡与反向代理 部分内容. 当我们的应用单实例不能支撑用户请求时,此时就需要扩容,从一台服务器扩容到两台.几十台.几百台.然而,用 ...

  2. Nginx负载均衡与反向代理—《亿级流量网站架构核心技术》

    当我们的应用单实例不能支撑用户请求时,此时就需要扩容,从一台服务器扩容到两台.几十台.几百台.然而,用户访问时是通过如http://www.XX.com的方式访问,在请求时,浏览器首先会查询DNS服务 ...

  3. Nginx 负载均衡和反向代理实践

    nginx 以哪个配置文件启动 Nginx 负载均衡和反向代理实践 环境介绍 192.168.1.50    在这台主机上配置Nginx 的反向代理,负载均衡,和web1,web1使用的81号端口 1 ...

  4. CentOS中实现Nginx负载均衡和反向代理

    一.安装必要软件 负载均衡服务器:IP设置为192.168.1.10 Web服务器1:安装Apache或者Nginx,IP设置为192.168.1.11: Web服务器2:安装Apache或者Ngin ...

  5. nginx负载均衡和反向代理有什么区别

    近在研究nginx的负载均衡和反向代理,先看下这两个简单的配置吧! 负载均衡 worker_processes 1; events { worker_connections 1024; } http{ ...

  6. Nginx负载均衡和反向代理设置

    Nginx负载均衡: 格式: upstream 别名 {    #别名一般要有意义,能看出是做什么的 server ip:端口;    #要实现负载的服务器的ip.端口号}  例: upstream ...

  7. Nginx负载均衡和反向代理的配置和优化

    负载均衡 负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台服务器都具有等价的地位, 反向代理 是指以代理服务器来接受internet上的请求,然后将请求转给内部的服务器 常见的负载均衡 1. ...

  8. CentOS中nginx负载均衡和反向代理的搭建

    1: 修改centos命令行启动(减少内存占用): vim /etc/inittab :initdefault: --> 修改5为3 若要界面启动使用 startx 2:安装jdk )解压:jd ...

  9. nginx 负载均衡及反向代理

    Nginx简介 Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师开发,官方测试nginx能够支支撑5万并发链接,并且cpu.内存 ...

  10. Nginx负载均衡与反向代理的配置实例

    user www www; worker_processes 10; error_log /data1/logs/nginx_error.log crit; pid /usr/local/webser ...

随机推荐

  1. 探索专有领域的端到端ASR解决之道

    摘要:本文从<Shallow-Fusion End-to-End Contextual Biasing>入手,探索解决专有领域的端到端ASR. 本文分享自华为云社区<语境偏移如何解决 ...

  2. [bug] Failed building wheel for xxx

    参考 https://blog.csdn.net/pengzhisen123/article/details/79049834 https://www.lfd.uci.edu/~gohlke/pyth ...

  3. [Linux]常用命令、组合命令以及输入输出重定向

    [Linux]常用命令.组合命令以及输入输出重定向 2020-03-10阅读 1580   原创文章 文章目录 0.切换目录 1.复制文件和目录`cp` 1.1.复制文件 1.2.复制目录 1.3.扩 ...

  4. systemctl服务------字符和图像界面切换systemctl set-default multi-user.target systemctl isolate multi-user.target #当前立即进入字符模式 [root@room4pc09 桌面]# systemctl isolate graphical.target #当前立即进入图形模式

    查看服务运行状态 [root@room4pc09 桌面]# systemctl status crond #查看服务运行状态 ● crond.service - Command Scheduler L ...

  5. 获取显卡硬件信息lspci -vnn | grep VGA -A 12

    lspci -vnn | grep VGA -A 12 lshw -C display lshw -c video | grep configuration glxinfo | grep OpenGL ...

  6. Ansible_常用文件模块使用详解

    一.Ansibel常用文件模块使用详解 1.file模块 1️⃣:file模块常用的参数列表: path       被管理文件的路径 state状态常用参数: absent           删除 ...

  7. ELK学习实验014:Nginx日志JSON格式收集

    1 Kibana的显示配置 https://demo.elastic.co/app/kibana#/dashboard/welcome_dashboard 环境先处理干净 安装nginx和httpd- ...

  8. Hibernate使用原生SQL语句进行无关联多表查询

    背景:有两个表:CpCg与CpGg需要进行多表查询 因为CpGg表设计到与另外的表也有联系,因此师兄没有关联此两个表,只是用字段进行逻辑关联,CpGg表的cp字段与CpCg表的id字段逻辑关联

  9. SSH工作过程简介和SSH协议算法集简介

    SSH简介 SSH是Secure Shell(安全外壳)的简称,是一种在不安全的网络环境中,通过加密机制和认证机制,实现安全的远程访问以及文件传输等业务的网络安全协议. SSH协议采用了典型的客户端/ ...

  10. 项目实践之工作流引擎基本文档!Activiti工作流框架中流程引擎API和服务详解

    流程引擎的API和服务 流程引擎API(ProcessEngine API)是与Activiti打交道的最常用方式 Activiti从ProcessEngine开始.在ProcessEngine中,可 ...