Nginx的部分配置
?
1.?基礎容災配置(被動健康檢查)
在?upstream
?塊中,通過?max_fails
?和?fail_timeout
?參數定義故障轉移規則:
在?upstream
?塊中,通過?max_fails
?和?fail_timeout
?參數定義故障轉移規則:
nginx
復制
upstream backend {# 定義后端服務器,設置失敗閾值和超時server 172.16.108.42:80 max_fails=3 fail_timeout=10s; # 10秒內失敗3次則標記為不可用server 172.16.108.43:80 max_fails=3 fail_timeout=10s;# 負載均衡策略(可選)least_conn; # 最少連接數策略 }
參數說明:
-
max_fails
:在?fail_timeout
?時間內,允許的最大失敗請求次數。 -
fail_timeout
:服務器被標記為不可用的時間(超時后自動恢復探測)。 -
1.?權重(
weight
)的作用 -
默認值:如果未指定?
weight
,默認值為?1
。 -
流量分配規則:根據權重值的比例分配請求。
-
適用場景:后端服務器性能不均衡時(如一臺性能強、一臺性能弱),通過權重調整流量分配。
效果:
-
當某臺服務器連續失敗 3 次后,Nginx 會將其標記為不可用,10秒內不再分配請求。
-
10秒后,Nginx 會嘗試重新發送請求探測是否恢復。
http {# ... 其他原有配置 ...upstream ai-backend {server 172.16.108.42:10011 weight=1 max_fails=3 fail_timeout=10s;server 172.16.108.43:10011 weight=1 max_fails=3 fail_timeout=10s;keepalive 32;least_conn;}server {listen 10011;server_name 172.16.108.41;location / {proxy_pass http://ai-backend;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 超時時間設為20分鐘proxy_connect_timeout 1200s;proxy_read_timeout 1200s;proxy_send_timeout 1200s;}}# ... 其他服務配置 ...
}
http {# ... 原有其他配置(如AI服務、日志格式等) ...# 定義詞向量服務的upstreamupstream wordvec-backend {server 172.16.108.44:10011 weight=1 max_fails=3 fail_timeout=10s; # 本地Nginx服務器的10011端口(若服務部署在Nginx本機)server 172.16.108.42:10011 weight=1 max_fails=3 fail_timeout=10s; # 另一臺服務器的10011端口keepalive 32; # 保持長連接least_conn; # 最少連接數負載均衡}# 詞向量服務的獨立監聽端口(例如10012)server {listen 6001; # 監聽外部請求的端口server_name 172.16.108.41; # Nginx服務器IP或域名location / {proxy_pass http://wordvec-backend; # 轉發到詞向量后端proxy_http_version 1.1;proxy_set_header Connection ""; # 啟用HTTP 1.1長連接proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# 超時時間設為20分鐘(1200秒)proxy_connect_timeout 1200s; # 連接后端超時proxy_read_timeout 1200s; # 讀取響應超時proxy_send_timeout 1200s; # 發送請求超時}}
}
1.?檢查配置文件語法
在重啟前?必須驗證配置正確性,避免錯誤配置導致服務崩潰:
sudo nginx -t
nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:57
nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:64
nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:71
nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:79
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
2.?重新加載配置(平滑重啟)
傳統方式:
sudo nginx -s reload[root@bigdata41 nginx]# sudo nginx -s reload nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:57 nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:64 nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:71 nginx: [warn] load balancing method redefined in /etc/nginx/nginx.conf:79 nginx: [error] invalid PID number "" in "/run/nginx.pid"報異常 暫未處理3.?完全重啟服務(強制重啟)
sudo systemctl restart nginx
sudo systemctl?restart?nginx??重新啟動nginx
sudo systemctl status nginx??查看nginx?狀態
將41上已有的6001端口服務遷移走
?sudo systemctl status nginx