前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于CentOS 7配置Nginx负载均衡

基于CentOS 7配置Nginx负载均衡

作者头像
Leshami
发布2018-08-06 10:11:22
1K0
发布2018-08-06 10:11:22
举报
文章被收录于专栏:乐沙弥的世界乐沙弥的世界

Nginx负载均衡是Nginx的核心功能之一,工作在第七层。它是除了lvs,haproxy之外市面上较为流行的一种负载均衡软件。可以将客户端请求分流到跨多个计算资源(如计算机,计算机集群,网络链接,中央处理单元或磁盘驱动器)的工作负载分布。负载均衡旨在优化资源使用,最大化吞吐量,最小化响应时间,并避免任何单一资源的过载。使用具有负载平衡的多个组件而不是单个组件可以通过冗余来提高可靠性和可用性。本文简要描述Nginx负载均衡的配置,供大家参考。

一、负载均衡upstream模块介绍

upstream模块可定义一个新的上下文,它包含了一组后端upstream服务器,这些服务器可能被赋予了不同的权重、不同的类型甚至可以基于维护等原因被标记为down。 upstream语法及示例   语法:upstream name { … }   声明一组可以被proxy_pass和fastcgi_pass引用的服务器;这些服务器可以使用不同的端口,并且也可以使用Unix Socket;也可以为服务器指定不同的权重; 例如: upstream backend { server backend1.example.com weight=5 down backup; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend2; }

upstream模块常用的指令有: ip_hash   基于客户端IP地址完成请求的分发,它可以保证来自于同一个客户端的请求始终被转发至同一个upstream服务器; keepalive   每个worker进程为发送到upstream服务器的连接所缓存的个数; least_conn   最少连接调度算法; server   定义一个upstream服务器的地址,还可包括一系列可选参数,如:     weight:权重;     max_fails:最大失败连接次数,失败连接的超时时长由fail_timeout指定;     fail_timeout:等待请求的目标服务器发送响应的时长;     backup:用于fallback的目的,所有服务均故障时才启动此服务器;     down:手动标记其不再处理任何请求;

upstream模块的负载均衡轮询算法

轮询(round-robin 默认)   每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 权重(weight)   指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 IP哈希(ip_hash)   每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 第三方(fair)   按后端服务器的响应时间来分配请求,响应时间短的优先分配。 第三方(url_hash)

二、后端服务器1配置

代码语言:javascript
复制
说明,当前的演示环境中,前后端全部使用Nginx。
查看主机名及IP
# hostname
centos7-web.example.com
# ip addr|grep inet|grep global
inet 172.24.8.128/24 brd 172.24.8.255 scope global eno16777728

Nginx版本
# nginx -v
nginx version: nginx/1.9.0

添加测试文件
# mv /etc/nginx/html/index.html /etc/nginx/html/index.html.bk
# echo "This a test home page from 172.24.8.128">/etc/nginx/html/index.html

# ss -nltp|grep nginx
LISTEN 0 128 *:90 *:* users:(("nginx",pid=2399,fd=6),("nginx",pid=2398,fd=6))

# curl http://localhost:90
This a test home page from 172.24.8.128

三、后端服务器2配置

代码语言:javascript
复制
查看主机名及IP
# hostname
node132
# ip addr|grep inet|grep global
inet 192.168.1.132/24 brd 192.168.1.255 scope global eth0

Nginx版本
# nginx -v
nginx version: nginx/1.10.2

添加测试文件
# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bk
# echo "This a test home page from 192.168.1.132">/usr/share/nginx/html/index.html

# ss -nltp|grep nginx
LISTEN 0 128 :::80 :::* users:(("nginx",2808,7),("nginx",6992,7))
#
# curl http://localhost
This a test home page from 192.168.1.132

四、Nginx负载均衡配置及验证

代码语言:javascript
复制
负载均衡主机名及IP
# hostname
centos7-router
# ip addr|grep inet|grep global
inet 172.24.8.254/24 brd 172.24.8.255 scope global eno16777728
inet 192.168.1.175/24 brd 192.168.1.255 scope global dynamic eno33554960

Nginx版本
# nginx -v
nginx version: nginx/1.12.2

负载均衡配置
# vim /etc/nginx/conf.d/slb.conf
upstream www {
    server 172.24.8.128:90 max_fails=3 fail_timeout=30s;
    server 192.168.1.132:80 max_fails=3 fail_timeout=30s;
    keepalive 32;
}

server {

    listen 9090;
    server_name localhost;

    location / {
        proxy_set_header Host $host;
        proxy_set_header x-for $remote_addr;
        proxy_set_header x-server $host;
        proxy_set_header x-agent $http_user_agent;
        proxy_pass http://www;
}
}

验证负载均衡效果
# systemctl reload nginx
# curl http://localhost:9090
This a test home page from 172.24.8.128
# curl http://localhost:9090
This a test home page from 192.168.1.132
# curl http://localhost:9090
This a test home page from 172.24.8.128
# curl http://localhost:9090
This a test home page from 192.168.1.132

配置IP hash轮询策略,修改后的部分内容如下
# head -n6 /etc/nginx/conf.d/slb.conf
upstream www {
    ip_hash;
    server 172.24.8.128:90 max_fails=3 fail_timeout=30s;
    server 192.168.1.132:80 max_fails=3 fail_timeout=30s;
    keepalive 32;
    ....
}

# systemctl reload nginx
# curl http://localhost:9090
This a test home page from 172.24.8.128
# curl http://localhost:9090
This a test home page from 172.24.8.128
# curl http://localhost:9090
This a test home page from 172.24.8.128

经测试ip_hash轮询还是存在一定的问题,就是不论在那台机器访问,始终请求到第一台机器。 之前也做过一次测试没有成功。生产环境后来换成了Tengine。 version: Tengine/2.1.2 (nginx/1.6.2) 在Tengine中使用session_sticky指令实现session粘滞。 关于这个ip_hash,有网友描述了这个原因:不论A类B类C类等网络地址,Nginx的ip_hash算法都将一个ip地址的前三段作为hash的关键字。 Nginx的ip_hash指令 http://blog.csdn.net/fygkchina/article/details/41841915

五、基于Nginx代理到tomcat的配置(演示略)

代码语言:javascript
复制
# more tomcat.conf 
upstream app {
                ip_hash;
                server 192.168.81.146:8080;
                server 192.168.81.147:8080;
                keepalive 32;
}

server {
    listen 80;
    server_name localhost;
    location / {
        proxy_pass http://app;

        proxy_set_header Host $http_host;   #Auhtor : Leshami
        proxy_set_header X-Real-IP $remote_addr;  #Blog : http://blog.csdn.net/leshami
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-scheme $scheme;
        proxy_set_header x-agent $http_user_agent;
    }

server {
    listen      443 ssl;
    server_name  localhost;
    server_name node132.ydq.com;
    ssl_certificate      /etc/nginx/conf.d/node132.ydq.com.crt;
    ssl_certificate_key  /etc/nginx/conf.d/node132.ydq.com.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        proxy_pass http://app;

        proxy_set_header Host $http_host;
        proxy_set_header X-Real-Port $remote_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-scheme $scheme;
        proxy_set_header x-agent $http_user_agent;
        add_header backendIP $upstream_addr;
        proxy_set_header Proxy_Port $proxy_port;
    }
} 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年12月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、负载均衡upstream模块介绍
  • 二、后端服务器1配置
  • 三、后端服务器2配置
  • 四、Nginx负载均衡配置及验证
  • 五、基于Nginx代理到tomcat的配置(演示略)
相关产品与服务
负载均衡
负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档