您好,登錄后才能下訂單哦!
下文給大家帶來負載均衡Nginx配置流程及步驟介紹,希望能夠給大家在實際運用中帶來一定的幫助,負載均衡涉及的東西比較多,理論也不多,網上有很多書籍,今天我們就用億速云在行業內累計的經驗來做一個解答。
192.168.1.150 VOIP 虛擬IP
192.168.1.151 CNSHANK01 負載均衡1
192.168.1.152 CNSHANK02 負載均衡2
系統:Centos7.4
一、nginx配置(兩臺nginx配置一模一樣,主頁設置不同):
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 {
}
}
}
設置配置主頁測試文件
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上
二、keepalived配置:
I、主服務器配置(192.168.1.151):
配置:
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"
}
添加服務檢查腳本(check.sh):
vim /etc/keepalived/check.sh
#!/bin/bash
if [ "pgrep nginx | wc -l
" == "0" ] ; then
exit 1
else
exit 0
fi
date '+%F %T'
:切換到主機模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:切換到備機模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:宕機" >> /var/log/keepalived/keepalived.log給腳本添加可執行權限:
chmod +x /etc/keepalived/check.sh
chmod +x /etc/keepalived/notify.sh
創建日志目錄:
mkdir /var/log/keepalived
II、備服務器配置(192.168.1.152):
安裝:
yum -y install keepalived
配置:
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"
}
pgrep nginx | wc -l
" == "0" ] ; thendate '+%F %T'
:切換到主機模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:切換到備機模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:宕機" >> /var/log/keepalived/keepalived.logII、停止主服務器:192.168.1.151后測試
用域名測試
修改本地hosts文件C:\Windows\System32\drivers\etc\hosts文件加入
192.168.1.150 www.bbs.com
看了以上關于負載均衡Nginx配置流程及步驟介紹,如果大家還有什么地方需要了解的可以在億速云行業資訊里查找自己感興趣的或者找我們的專業技術工程師解答的,億速云技術工程師在行業內擁有十幾年的經驗了。億速云官網鏈接www.mlszssj.com
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。