中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

負載均衡Nginx配置流程及步驟介紹

發布時間:2020-04-16 11:29:03 來源:億速云 閱讀:472 作者:三月 欄目:系統運維

下文給大家帶來負載均衡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.5655pk.com

 

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

九江市| 利津县| 湖南省| 玉门市| 鹤峰县| 凌源市| 虹口区| 大洼县| 如皋市| 河间市| 墨脱县| 通许县| 淳安县| 威远县| 阜阳市| 商洛市| 定陶县| 呈贡县| 娄烦县| 巴中市| 秦皇岛市| 五大连池市| 博乐市| 永年县| 汨罗市| 革吉县| 华坪县| 萨嘎县| 定州市| 固阳县| 佳木斯市| 栖霞市| 微山县| 株洲市| 铜陵市| 新安县| 天台县| 收藏| 沐川县| 邛崃市| 茌平县|