负载均衡Nginx配置流程及步骤介绍-成都快上网建站

负载均衡Nginx配置流程及步骤介绍

下文给大家带来负载均衡Nginx配置流程及步骤介绍,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用创新互联在行业内累计的经验来做一个解答。

创新互联建站致力于成都网站设计、成都网站制作、外贸网站建设,成都网站设计,集团网站建设等服务标准化,推过标准化降低中小企业的建站的成本,并持续提升建站的定制化服务水平进行质量交付,让企业网站从市场竞争中脱颖而出。 选择创新互联建站,就选择了安全、稳定、美观的网站建设服务!

192.168.1.150   VOIP    虚拟IP
192.168.1.151   CNSHANK01   负载均衡1
192.168.1.152   CNSHANK02   负载均衡2
系统:Centos7.4

负载均衡Nginx配置流程及步骤介绍

一、nginx配置(两台nginx配置一模一样,主页设置不同):

  1. 安装:
    yum  -y  install  nginx
  2. 配置:
    vim /etc/nginx/nginx.conf
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    include /usr/share/nginx/modules/*.conf;
    events {
    worker_connections 12800;
    use epoll;
    }
    http {

include mime.types;
default_type  application/octet-stream;
limit_conn_zone $binary_remote_addr zone=addr:10m; #防IP***

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  /var/log/nginx/access.log  main;
sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;
include /etc/nginx/mime.types; #协助部分浏览器(如firefox)识别网页文件的类型
default_type        application/octet-stream;
include /etc/nginx/conf.d/*.conf;

client_body_buffer_size 1k;  #指定客户端请求主体缓冲区大小
client_header_buffer_size 1k;  #指定来自客户端请求头的headerbuffer大小
large_client_header_buffers 2 1k; #客户端请求中较大的消息头指定的缓存最大数量和大小
server_tokens off;  #禁止在错误页面上显示nginx版本号
client_body_timeout 20;  #设置客户端请求主体读取超时时间
client_header_timeout 20; #设置客户端请求头读取超时时间
gzip  on;

upstream dynamic {   #负载云服务器名dynamic
ip hash;    #采用ip_hash方式
server 192.168.1.154:80;  
server 192.168.1.155:80;
}
server {
listen       80 default_server;
listen       [::]:80 default_server;
server_name  blog.dollarphp.com;
root         /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://dynamic;  #负载均衡 代理
limit_conn addr 100;   #指定每个ip最多只允许建立100个连接
limit_rate 500k;         #每个ip最大带宽是500k
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;

    index index.html index.htm;
    autoindex on;
    autoindex_localtime on;
}
location ~* ^.+.(js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
    root html;
}
location ~* ^.+.(?![js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma])$ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
}
error_page 404 /404.html;
    location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
    location = /50x.html {
}

}
}

  1. 设置配置主页测试文件
    cd /usr/share/nginx/html
    cp ./index.html ./index.html.bak
    echo “CNSHANK01-192.168.1.151”>./index.html #在主机192.1681.151上
    echo “CNSHANK02-192.168.1.152”>./index.html #在主机192.1681.152上

  2. 启动
    systemctl  start  nginx
    systemctl  enable  nginx

二、keepalived配置:
I、主服务器配置(192.168.1.151):

  1. 安装:
    yum  -y  install  keepalived
  2. 配置:
    vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
    }
    vrrp_script chk_nginx {
    script "/etc/keepalived/check.sh"
    interval 1
    weight -15
    }
    vrrp_instance VI_1 {  #  实例,一个vip一个实例
    state MASTER    #标识为主服务
    interface ens33
    virtual_router_id 51  #与从机保持一致即可
    priority 100 #权重 ,高于从机
    advert_int 1
    authentication {    # 认证授权,设置验证信息,两个节点必须一致
    auth_type PASS
    auth_pass 123456
    }
    virtual_ipaddress {  #虚拟ip地址
    192.168.1.150
    }
    track_script { #nginx监控服务
    chk_nginx
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
    }

  3. 添加服务检查脚本(check.sh):
    vim /etc/keepalived/check.sh
    #!/bin/bash
    if [ "pgrep  nginx  |  wc  -l" == "0" ] ; then
    exit 1
    else
    exit 0
    fi

  4. 通知脚本(notify.sh):
    vim /etc/keepalived/notify.sh
    #!/bin/bash
    if [ "$1" == "master" ] ; then
    echo "date '+%F %T':切换到主机模式" >> /var/log/keepalived/keepalived.log
    elif [ "$1" == "backup" ] ; then
    /usr/bin/systemctl start nginx
    echo "date '+%F %T':切换到备机模式" >> /var/log/keepalived/keepalived.log
    else
    /usr/bin/systemctl start nginx
    echo "date '+%F %T':宕机" >> /var/log/keepalived/keepalived.log
    fi
  5. 给脚本添加可执行权限:
    chmod  +x  /etc/keepalived/check.sh
    chmod  +x  /etc/keepalived/notify.sh

  6. 创建日志目录:
    mkdir  /var/log/keepalived

  7. 启动:
    systemctl  start  keepalived

II、备服务器配置(192.168.1.152):

  1. 安装:
    yum  -y  install  keepalived

  2. 配置:
    vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
    }
    vrrp_script chk_nginx {
    script "/etc/keepalived/check.sh"
    interval 1
    weight -15
    }
    vrrp_instance VI_1 {
    state BACKUP    #BACKUP模式
    interface ens33
    virtual_router_id 51    #与主机一致
    priority 99 #比主机小
    advert_int 1
    authentication {    #与主机一致
    auth_type PASS
    auth_pass 123456
    }
    virtual_ipaddress {
    192.168.1.150
    }
    track_script {
    chk_nginx
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
    }

  3. 添加服务检查脚本(check.sh):
    vim /etc/keepalived/check.sh
    #!/bin/bash
    if [ "pgrep  nginx  |  wc  -l" == "0" ] ; then
    exit 1
    else
    exit 0
    fi
  4. 通知脚本(notify.sh):
    vim /etc/keepalived/notify.sh
    #!/bin/bash
    if [ "$1" == "master" ] ; then
    echo "date '+%F %T':切换到主机模式" >> /var/log/keepalived/keepalived.log
    elif [ "$1" == "backup" ] ; then
    /usr/bin/systemctl start nginx
    echo "date '+%F %T':切换到备机模式" >> /var/log/keepalived/keepalived.log
    else
    /usr/bin/systemctl start nginx
    echo "date '+%F %T':宕机" >> /var/log/keepalived/keepalived.log
    fi
  5. 给脚本添加可执行权限:
    chmod  +x  /etc/keepalived/check.sh
    chmod  +x  /etc/keepalived/notify.sh
  6. 创建日志目录:
    mkdir  /var/log/keepalived
  7. 启动:
    systemctl  start  keepalived
    三、测试:
    I、正常状态下
    负载均衡Nginx配置流程及步骤介绍

II、停止主服务器:192.168.1.151后测试

  1. 停止从服务器nginx服务,结果可以看到访问虚拟IP时候会从主服务器切到从服务器
    systemctl  stop  nginx
    负载均衡Nginx配置流程及步骤介绍
    负载均衡Nginx配置流程及步骤介绍
    负载均衡Nginx配置流程及步骤介绍
  2. 查看日志:
    tail  /var/log/keepalived/keepalived.log
    主从机日志都显示切到备机模式
    负载均衡Nginx配置流程及步骤介绍
    负载均衡Nginx配置流程及步骤介绍

用域名测试
修改本地hosts文件C:\Windows\System32\drivers\etc\hosts文件加入
192.168.1.150 www.bbs.com
负载均衡Nginx配置流程及步骤介绍
负载均衡Nginx配置流程及步骤介绍

看了以上关于负载均衡Nginx配置流程及步骤介绍,如果大家还有什么地方需要了解的可以在创新互联行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,创新互联技术工程师在行业内拥有十几年的经验了。创新互联官网链接www.cdcxhl.com

 


网站标题:负载均衡Nginx配置流程及步骤介绍
本文网址:http://kswjz.com/article/peehoh.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流