`
frank1998819
  • 浏览: 751723 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类

nginx负载均衡一配置(转)

 
阅读更多

 

nginx官网关于负载均衡模块的文档帮助:链接

1.nginx配置文件

[root@lb01 ~]# cd /application/nginx-1.6.2/
[root@lb01 nginx-1.6.2]# egrep -v "^$|#" conf/nginx.conf.default > conf/nginx.conf
  • 1
  • 2

2.官方示例配置及解释

upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
upstream:模块 不允许修改
backend:名称 可修改,我使用带有“_”的符号名字,会报400,原因不知。
server backend1.example.com: 写监听的域名或者ip
weight:权重,权重越大,分发到的任务越多
server unix:/tmp/backend3:可以使用socket
server backup1.example.com:8080   backup:热备,nginx自带的高可用,当上面监听的
两个服务器都挂掉了,就由热备的提供服务。
proxy_pass http://backend: 使用负载均衡
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

3.实际配置

[root@lb01 nginx-1.6.2]# cat -n conf/nginx.conf
     1  worker_processes  1;
     2  events {
     3      worker_connections  1024;
     4  }
     5  http {
     6      include       mime.types;
     7      default_type  application/octet-stream;
     8      sendfile        on;
     9      upstream lbproxy {
    10      server 10.0.0.12:80 weight=5;
    11      server 10.0.0.13:80 weight=5;
    12      server 10.0.0.11:80 backup;
    13      }
    14      keepalive_timeout  65;
    15      server {
    16          listen       80;
    17          server_name  lb.liang.com;
    18          location / {
    19              root   html;
    20              index  index.html index.htm;
    21              proxy_pass http://lbproxy;
    22          }
    23      }
    24  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

4.检查语法,重启nginx

[root@lb01 nginx-1.6.2]# nginx -t
nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful
[root@lb01 nginx-1.6.2]# nginx -s reload
  • 1
  • 2
  • 3
  • 4

5.测试

[root@lb01 nginx-1.6.2]# echo "10.0.0.10 lb.liang.com" >> /etc/hosts
[root@lb01 nginx-1.6.2]# for num in `seq 10`;do curl lb.liang.com;done
10.0.0.12
10.0.0.13
10.0.0.12
10.0.0.13
10.0.0.12
10.0.0.13
10.0.0.12
10.0.0.13
10.0.0.12
10.0.0.13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

6.修改权重测试

[root@lb01 nginx-1.6.2]# vim conf/nginx.conf
 10     server 10.0.0.12:80 weight=1;
 11     server 10.0.0.13:80 weight=5;
 12     server 10.0.0.11:80 backup;
 13     }
[root@lb01 nginx-1.6.2]# nginx -s reload  
[root@lb01 nginx-1.6.2]# for num in `seq 10`;do curl lb.liang.com;done
10.0.0.13
10.0.0.13
10.0.0.12
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.12
10.0.0.13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

7.ip_hash(使用宇保持会话)

7.1使用ip_hash一个ip只会调用到一台服务器

 10     ip_hash;
 11     server 10.0.0.12:80 weight=1;
 12     server 10.0.0.13:80 weight=5;
 13     server 10.0.0.11:80 backup;
 14     }
  • 1
  • 2
  • 3
  • 4
  • 5

7.2这时候检查语法会有错

[root@lb01 nginx-1.6.2]# nginx -t
nginx: [emerg] invalid parameter "backup" in /usr/local/nginx-1.6.2/conf/nginx.conf:13
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test failed
  • 1
  • 2
  • 3

7.3需要删掉热备,ip_hash不支持热备

 10     ip_hash;
 11     server 10.0.0.12:80 weight=1;
 12     server 10.0.0.13:80 weight=5;
 13     #server 10.0.0.11:80 backup;
 14     }
  • 1
  • 2
  • 3
  • 4
  • 5

7.4重启,测试

[root@lb01 nginx-1.6.2]# nginx -s reload
[root@lb01 nginx-1.6.2]# for num in `seq 10`;do curl lb.liang.com;done
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13

 

分享到:
评论

相关推荐

    Nginx实现负载均衡 web均衡负载 webservice负载均衡 Nginx实现负载均衡配制全说明

    Nginx实现负载均衡 web均衡负载 webservice负载均衡 Nginx实现负载均衡配制全说明 为了多台后台的web、webservice服务能均衡负载,可以使用nginx进行处理 1)配置文件全配制ok 2)有两个完整的web服务做例子,可以...

    Nginx负载均衡配置

    下面详细解释Nginx负载均衡的配置方法以及负载均衡策略。 首先,要配置Nginx与Tomcat实现负载均衡,需要准备两个Tomcat实例,每个实例可以部署一个简单的Web项目,例如通过在页面上标注不同的端口号,以便区分它们...

    nginx负载均衡配置文件实例

    一个基本的Nginx负载均衡配置通常包含以下部分: 1. **upstream块**:定义一组后端服务器,并设置负载均衡策略。 2. **server块**:在http、server或location上下文中,引用upstream块,并配置相应的反向代理规则。...

    nginx负载均衡配置,宕机自动切换方式

    在传统的nginx负载均衡配置中,并未直接提供后端服务器健康检查的功能,但nginx提供了几个重要的指令来帮助我们设置超时和重试机制,来间接保障服务的可用性。例如,proxy_connect_timeout指令用于设置nginx尝试连接...

    nginx负载均衡配置-windows.docx

    本文档提供了一个完整的 Nginx 负载均衡配置示例,涵盖了基本配置、负载均衡配置、SSL 加密配置和 URL 路由规则配置等方面。通过阅读本文档,读者可以了解 Nginx 负载均衡的基本原理和配置方法,从而更好地应用 ...

    nginx负载均衡实现

    ### Nginx负载均衡实现 #### 一、负载均衡概念及必要性 负载均衡是一种用于在网络环境中分散工作负载的技术,通常用于改善网络性能、提高可用性和最大化资源利用。当单台服务器难以应对高流量和并发请求时,负载...

    nginx 负载均衡与缓存服务器标准配置文件

    nginx 负载均衡与缓存服务器标准配置文件

    nginx负载均衡配置文件demo

    **Nginx负载均衡配置详解** 在高并发的互联网应用环境中,服务器的性能优化和负载分发至关重要。这里我们探讨的是如何使用Nginx作为反向代理和负载均衡器,来提升系统的整体处理能力,特别是在处理静态内容和优化...

    nginx负载均衡的参考配置及https的参考配置

    nginx进行负载均衡的配置参考示例,及nginx支持https请求的配置参考示例

    nginx负载均衡的配置

    nginx负载均衡的配置,二级域名的配置

    nginx配置 +负载均衡+https协议

    - 对于负载均衡,可以通过在Nginx配置文件中定义多个后端服务器,并使用`proxy_pass`指令来实现。 ```nginx upstream backend { server backend1.example.com; server backend2.example.com; } server { ...

    Linux下nginx负载均衡

    本文将详细解析"Linux下Nginx负载均衡"这一主题,包括Nginx的基本概念、配置原理以及如何在Linux系统中设置负载均衡。 Nginx是一款高性能的HTTP和反向代理服务器,同时也是一款邮件协议代理服务器。它的主要特点是...

    nginx负载均衡配置

    nginx+tomcat配置实现负载均衡,亲测可用

    nginx负载均衡架构配置

    nginx负载均衡架构配置,nginx是架构中不可缺少的一环,通过nginx可以实现负载均衡

    nginx负载均衡部署

    【标题】:“Nginx负载均衡部署” 在现代Web服务架构中,负载均衡扮演着至关重要的角色,它能够有效地分发网络流量,提高系统可用性和响应速度,防止单点故障。Nginx作为一款高性能的HTTP和反向代理服务器,常常被...

    Nginx负载均衡搭建及配置技术

    Nginx 支持多种操作系统,包括 CentOS 7.2,本文将介绍如何在 CentOS 7.2 上搭建 Nginx 负载均衡配置。 在开始之前,确保系统网络正常、yum 可用,并关闭 iptables 和 selinux,因为这些因素可能对 Nginx 的安装和...

    nginx负载均衡文档

    linux下安装Nginx并做到负载均衡,配置详细在文档有介绍!

Global site tag (gtag.js) - Google Analytics