配置语法

Syntax:upstream name {...}

Default:——

Context:http

演示

准备两台虚拟主机192.168.96.188、192.168.96.188

在188这台主机上创建3个html静态文件

vi /opt/app/code1/index.html

<html>
<head>
<meta charset="utf-8">
<title>server1</title>
</head>
<body style="">
<h1>Server 1<h1>
</body>
</html>

vi /opt/app/code2/index.html

<html>
<head>
<meta charset="utf-8">
<title>server2</title>
</head>
<body style="">
<h1>Server 2<h1>
</body>
</html>

vi /opt/app/code3/index.html

<html>
<head>
<meta charset="utf-8">
<title>server3</title>
</head>
<body style="">
<h1>Server 3<h1>
</body>
</html>

在188这台主机上创建3个.conf配置文件。配置如下

 vi /etc/nginx/conf.d/server1.conf

server {
listen 8001; #请求的8001端口
server_name localhost; #charset koi8-r;
access_log /var/log/nginx/server1.access.log main; location / {
root /opt/app/code1; #静态文件目录
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/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;
#}
}

vi /etc/nginx/conf.d/server2.conf

server {
listen 8002; #请求的8002端口
server_name localhost; #charset koi8-r;
access_log /var/log/nginx/server1.access.log main; location / {
root /opt/app/code2; #静态文件目录
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/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;
#}
}

 vi /etc/nginx/conf.d/server3.conf

server {
listen 8003; #请求的8003端口
server_name localhost; #charset koi8-r;
access_log /var/log/nginx/server1.access.log main; location / {
root /opt/app/code3; #静态文件目录
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/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;
#}
}

在135这台主机上配置负载均衡

添加配置文件

vi /usr/local/nginx/conf/hosts/upstream.conf

 upstream test {
server 192.168.96.188:8001; # 添加 test 配置监听的ip+端口
server 192.168.96.188:8002;
server 192.168.96.188:8003; } server {
listen 80;
server_name localhost jeson.t.imooc.io; #charset koi8-r;
#access_log /usr/local/nginx/logs/test_proxy.access.log main;
resolver 8.8.8.8; location / {
proxy_pass http://test; # 添加需要监听 test
include proxy_params; # 存储语法的文件名称
} #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 /usr/share/nginx/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;
#}
}

在/usr/local/nginx/conf/目录新建文件

vi proxy_params   #对应楼上配置文件中的文件名称

proxy_redirect default;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr; proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60; proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k; proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

配置好两台主机的nginx。记得检查语法、重启nginx。

使用本地浏览器访问192.168.96.135,按F5刷新,会循环访问server1、server2、server3、

这是关闭主机188的8002端口

 iptables -I INPUT -p tcp --dport 8002 -j DROP

再次本地浏览器访问192.168.96.135,按F5刷新。这时候就不会有server2访问页面了

现实场景中,三台主机有一台主机宕机了,没有做负载均衡,会导致其他主机也无法访问。

有做负载均衡,一台主机宕机了,其他主机照样也能访问

Nginx作为负载均衡——实战演练的更多相关文章

  1. ASP.NET Core使用Docker-Swarm集群部署实现负载均衡实战演练

    一.需求背景 人生苦短,我用.NET Core!阿笨对Docker是这样评价的:Docker在手,环境我有!Docker出手,集群我有!前面的Doc基础课程我们学习了如何使用Docker来部署搭建单机 ...

  2. nginx+tomcat负载均衡

    最近练习nginx+tomcat负载均衡.根据一些资料整理了大体思路,最终实现了1个nginx+2个tomcat负载均衡. 安装JDK 1>进入安装目录,给所有用户添加可执行的权限 #chmod ...

  3. 使用Nginx实现负载均衡

    使用Nginx实现负载均衡 一.nginx简介 nginx是一个高性能的HTTP服务器和反向代理服务器.它起初是俄罗斯人Igor Sysoev开发的,至今支撑者俄罗斯的很多大型的网站. 二.nginx ...

  4. nginx作为负载均衡服务器——测试

    i. 需求 nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器. nginx负载均衡服务器:192.168.101.3 tomcat1服 ...

  5. windows配置nginx实现负载均衡集群

    windows配置nginx实现负载均衡集群2014-08-20 09:44:40   来源:www.abcde.cn   评论:0 点击:617 网上大部分关于nginx负载均衡集群的教程都是lin ...

  6. 使用nginx做负载均衡的session共享问题

    查了一些资料,看了一些别人写的文档,总结如下,实现nginx session的共享PHP服务器有多台,用nginx做负载均衡,这样同一个IP访问同一个页面会被分配到不同的服务器上,如果session不 ...

  7. 【Nginx】配置Nginx的负载均衡

    参考的优秀文章 tomcat配置文件server.xml详解 AJP协议总结与分析 Using nginx as HTTP load balancer 在本机运行2个Tomcat 现需要运行两个Tom ...

  8. Nginx的负载均衡 - 整体架构

    Nginx的负载均衡 - 整体架构 Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd Nginx目前提供的负载均衡模块: ngx_http_upstre ...

  9. Nginx作为负载均衡服务器(Windows环境)

    一个最简单的负载均衡测试,不涉及到session复制,只是将请求分配到不同的服务器上去而已. 1.创建一个简单的web应用.只有一个index.jsp页面,,内容如下. <%@ page lan ...

随机推荐

  1. 设置html各元素不可点击(持续更新)

    1.span <span id="nextStep" onclick="right">下一页</span> $("#nextS ...

  2. java编译器优化和运行期优化

    概述    最近在看jvm优化,总结一下学习的相关知识 (一)javac编译器 编译过程 1.解析与填充符号表过程 1).词法.语法分析    词法分析将源代码的字符流转变为标记集合,单个字符是程序编 ...

  3. activiti工作流引擎学习(三)

    5.接收任务活动(receiveTask,即等待活动)不是一个任务节点 接收任务是一个简单任务,他会等待回应消息的到达,当前,官方只实现了这个任务的java语义,当流程达到接受任务,流程状态会保存到数 ...

  4. ARM裸机开发之交叉工具链和MakeFile工程管理

    一.交叉工具链 嵌入式Linux开发采用交叉开发,简单来说就是在宿主机(PC机)上面编译出能够在其他硬件平台上面运行的程序.在这个过程中,需要用到许多的交叉工具,这些交叉工具的集合就叫做交叉工具链.下 ...

  5. Visio高级应用部件

    标注与公式的应用: 插入标注 怎么让标注与图形建立关联:拖动标注的时候坐下角会出现黄色的点 把标准拖动到形状边的时候让黄点进入形状就是建立了关联 然后标注就会随着形状的移动而移动 而且复制和删除也都是 ...

  6. 怎么彻底删除用友通T3财务软件?

    [问题现象]怎么彻底删除用友通T3财务软件? [原因分析]通过"添加或删除程序"无法正常卸载用友通T3,也尝试了360安全卫士强力卸载,都无法完全卸载,有没有办法可以彻底删除用友通 ...

  7. hexo+next 详细搭建

    安装node node下载地址:http://nodejs.cn/download/ 具体安装方法,这里不做详写 安装完成可以通过node -v 查看安装是否生效和node的版本 我这里使用的是v10 ...

  8. Android Drawable - Shape Drawable使用详解(附图)

    TIPS shape图形 –简单介绍 shape图形 –如何画? shape图形 –参数详细解析 shape图形 –如何用? shape图形 –实际开发应用场景 shape图形简单介绍 用xml实现一 ...

  9. JVM探秘:内存溢出

    本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. 在 Java 虚拟机内存区域中,除了程序计数器外,其他几个内存区域都可能会发生OutO ...

  10. JVM性能监测工具——VisualVM

    Java本身自带了有好几个jvm监测工具,其中jconsole和jvisualvm这两个工具具有图形化界面,可以监测到cpu.类.线程.堆等一些参数,而且具有远程监控的能力. 启动:打开cmd命令窗口 ...