前文鏈接
- 高可用系列文章之一 - 概述 - 東風微鳴技術博客 (ewhisper.cn)
- 高可用系列文章之二 - 傳統分層架構技術方案 - 東風微鳴技術博客 (ewhisper.cn)
四 NGINX 高可用實施方案
高可用的實施, 主要步驟概述如下:
- NGINX 的安裝及基礎配置
- 負載均衡層高可用: NGINX + Keepalived配置
- 應用服務層高可用: NGINX -> 應用服務層 轉發配置
系統軟 硬體詳細配置清單
根據制造業高可用架構設計, 以及業務需求, 部署模型建議配置如下:
- 負載均衡服務器(即 NGINX + Keepalived): 2臺, 作業系統 Linux. 配置建議如下:
| 名稱 | 規格 | 備注 |
|---|---|---|
| CPU | 2 core | |
| 記憶體 | 4 GB | |
| 硬碟 | 50 GB | |
| 作業系統 | SUSE12 64位及補丁 | |
| 網卡 | 至少1塊網卡, 支持VRRP 技術 |
- 軟體運行配置環境:
| 軟體 | 規格 | |
|---|---|---|
| NGINX | 1.16.1 | |
| Keepalived | 2.0.10 | |
4.1 NGINX 安裝及配置
4.1.1 磁區及目錄
建議至少分為以下3個區:
| 磁區及目錄 | 大小 | 備注 |
|---|---|---|
主磁區( /) |
默認 | nginx程式及組態檔位于該磁區(/etc/nginx) |
日志磁區(/var/log/nginx) |
10G-20G | |
程式目錄磁區(/usr/share/nginx/html) |
10G | 可選, nginx用作web server時需要用到此目錄. |
4.1.2 程式及依賴版本
| 程式組件 | 安裝包名 | 版本 | md5 |
|---|---|---|---|
| nginx | nginx-1.16.1-1.sles12.ngx.x86_64.rpm | 1.16.1 | 396A359F26DD0100CD59545BAFFAFE85 |
4.1.3 NGINX程式規范
- nginx程式目錄:
/etc/nginx - 執行程式路徑:
/usr/sbin/nginx - 主組態檔路徑:
/etc/nginx/conf/nginx.conf - 各個應用系統轉發組態檔目錄:
/etc/nginx/conf.d/ - 日志目錄:
/var/log/nginx - 各個應用系統靜態檔案目錄:
/usr/share/nginx/html
4.1.4 系統級別配置優化
? 注意:
需要
root用戶執行.
- 安裝組件:
logrotate - 修改連接數:
vi /etc/security/limits.conf
# vi編輯
* soft nofile 65535
* hard nofile 65535
- 修改系統內核配置:
vi /etc/sysctl.conf
# vi編輯
# NGINX Tuning Performance
fs.file-max = 65535
vm.zone_reclaim_mode = 0
net.core.somaxconn = 2048
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_mtu_probing = 1
# 生效
sysctl -p
4.1.5 配置NGINX repo
? 注意:
本節命令可以根據具體情況, 在公司內部 repo 倉庫機器上進行操作.
其他機器只需要配置內部 repo 地址即可.
鍵入以下 zypper 命令以添加 SLES 的 zypper 存盤庫
$ sudo zypper addrepo -G -t yum -c 'http://nginx.org/packages/sles/12' nginx

接下來,您必須驗證數字簽名以保持下載包的完整性和來源,使用wget命令獲取nginx簽名密鑰:
$ wget http://nginx.org/keys/nginx_signing.key
示例輸出:
--2020-01-09 23:48:48-- http://nginx.org/keys/nginx_signing.key
Resolving nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3, ...
Connecting to nginx.org (nginx.org)|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1561 (1.5K) [text/plain]
Saving to: ‘nginx_signing.key’
100%[==================================================>] 1,561 --.-K/s in 0s
2020-01-09 23:48:49 (117 MB/s) - ‘nginx_signing.key’ saved [1561/1561]
使用rpm命令將密鑰匯入rpm:
$ sudo rpm --import nginx_signing.key
4.1.6 SUSE 上安裝NGINX
鍵入以下 zypper 命令:
$ sudo zypper install nginx=1.16.1

4.1.7 可選: 配置防火墻
? 注意:
如果機房流量入口有其他專用防火墻, 則可以關閉 nginx 服務器上的防火墻, 且不需要執行此步驟.
首先創建Nginx特定服務的組態檔,使用vi命令等文本編輯器打開埠80:
$ sudo vi /etc/sysconfig/SuSEfirewall2.d/services/nginx
添加以下配置:
## Name: Nginx web server
## Description: Open ports for Nginx Server
# space separated list of allowed TCP ports
TCP="http"
(如果不需要 HTTPS 支持,則只需要允許 TCP 埠號 80 上的通信,)保存并退出 VI/VIM 文本編輯器,現在,只需運行以下命令打開埠80:
$ sudo yast firewall
必須使用 TAB 和箭頭鍵在 YaST 中跳轉,在 YaST 中,跳轉到允許的服務,然后按 Enter 鍵:

使用 TAB 跳轉到“Allowed Services”,然后按向下箭頭鍵選擇 Nginx web server,然后按回車鍵,必須按 Alt-A 來將Nginx服務器添加到防火墻:

按 Alt-N 和 Alt-F 保存并完成 SLES 上的防火墻設定,回傳 shell 提示符后,列出 sle 上的所有 iptables 規則:
$ sudo iptables -S
示例輸出:
-A input_ext -p tcp -m limit --limit 3/min -m tcp --dport 80 --tcp-flags FIN,SYN,RST,ACK SYN -j LOG --log-prefix "SFW2-INext-ACC-TCP " --log-tcp-options --log-ip-options
-A input_ext -p tcp -m tcp --dport 80 -j ACCEPT
使用 sudo 命令和 grep 命令的組合來確定埠 80 是否打開:
sudo sh -c 'iptables -L -n -v | grep :80'
4.1.8 啟動 NGINX Server
鍵入以下 systemctl 命令以在系統啟動時啟用 Nginx: (開機自啟)
$ sudo systemctl enable nginx
啟動 Nginx web 服務器:
$ sudo systemctl start nginx
驗證:
$ systemctl status nginx

要判斷 80 埠是否監聽, 運行以下 netstat 命令或 ss 命令:
$ sudo netstat -tulpn | grep :80
$ sudo ss -tulpn | grep :80

4.1.9 NGINX 基礎操作
停止:
$ sudo systemctl stop nginx
啟動:
$ sudo systemctl start nginx
重新啟動服務:
$ sudo systemctl restart nginx
更改配置后重新加載 Ngnix:
$ sudo systemctl reload nginx
?? 建議:
配置更新后使用
reload來重新加載nginx.
訪問 nginx 頁面:
假設 NGINX IP為: 192.168.0.1. 使用瀏覽器或 curl 訪問:
http://serve_IP
http://your-domain
http://192.168.0.1

$ curl -I 192.168.122.43
示例輸出:
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Sat, 03 Feb 2020 19:18:53 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 17 Oct 2019 13:30:50 GMT
Connection: keep-alive
ETag: "59e6060a-264"
Accept-Ranges: bytes
4.1.10 查找有關 SLES 上 Nginx 組態檔的資訊
現在 Nginx 已經啟動并運行了,接下來可以定制配置,
SLES 的服務器組態檔:
/etc/nginx/: nginx 默認配置目錄/etc/nginx/nginx.conf: nginx 主組態檔/etc/nginx/conf.d/default.conf: 默認 virtual host 的配置
修改主配置:
$ sudo vi /etc/nginx/nginx.conf
4.1.11 NGINX 日志
/var/log/nginx/access.log: 訪問日志/var/log/nginx/error.log: 錯誤日志
4.1.12 nginx.conf
主組態檔詳細說明如下:
#### 全域塊 開始 #####
# 配置允許運行Nginx服務器的用戶和用戶組
#user nginx nginx;
# 配置允許Nginx行程生成的worker process數
worker_processes 4;
#worker_cpu_affinity 0001 0010 0100 1000;
# 配置Nginx服務器運行時的錯誤日志檔案存放路徑和名稱
#error_log logs/error.log;
error_log logs/error.log info;
# 配置Nginx服務器運行時的pid檔案存放路徑和名稱
pid logs/nginx.pid;
#### 全域塊 結束 #####
#### events塊 開始 ####
events {
# 配置事件驅動模型
use epoll;
accept_mutex off;
multi_accept off;
worker_connections 65535;
}
#### events塊 結束 ####
#### http塊 開始 ####
http {
# 定義MIME-Type
include mime.types;
default_type application/octet-stream;
# 配置請求處理日志的格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent $request_time $upstream_response_time "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host"';
access_log logs/access.log main;
# 配置允許使用sendfile方式傳輸
sendfile on;
#tcp_nopush on;
# 配置連接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
# nginx允許的客戶端請求頭部的緩沖區大小
client_header_buffer_size 4k;
# gzip conf
gzip on;
gzip_min_length 1024;
gzip_buffers 32 4k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/xml image/x-icon image/svg+xml image/png text/css image/jpeg image/gif application/x-javascript application/javascript application/json;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
# security
port_in_redirect off;
server_tokens off;
# proxy buffer
proxy_buffers 8 4k;
proxy_buffer_size 4k;
proxy_temp_file_write_size 4k;
proxy_temp_path proxy_temp;
# proxy cache
# proxy_cache_path cache/ keys_zone=cache_all:10m;
#### server塊 開始 ####
## 配置虛擬主機localhost
server {
listen 80 reuseport;
server_name localhost;
#charset koi8-r;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#### server 塊 結束 ####
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# virtual hosts
include conf.d/default.conf;
}
#### http塊 結束 ####
?? 建議:
為了保證主組態檔的干凈. 建議通過
include conf.d/default.conf;類似這樣的方式來引入其他virtual hosts配置.
4.1.13 日志轉儲
-
sudo vi /etc/logrotate.d/nginx -
編輯內容:
/var/log/nginx/*.log { daily rotate 90 # 保留90天, 按需調整 create dateext #compress # 是否啟用壓縮, 按需調整 #minsize 1M #create 0644 nginx nginx # nginx日志所屬用戶和組, 按需調整 # copytruncate 用于還在打開中的日志檔案,把當前日志備份并截斷;是先拷貝再清空的方式,拷貝和清空之間有一個時間差,可能會丟失部分日志資料, # delaycompress 和compress 一起使用時,轉儲的日志檔案到下一次轉儲時才壓縮 missingok ifempty # default nomail #noolddir # default sharedscripts # 運行postrotate腳本,作用是在所有日志都輪轉后統一執行一次腳本,如果沒有配置這個,那么每個日志輪轉后都會執行一次腳本 postrotate # 在logrotate轉儲之后需要執行的指令,例如重新啟動 (kill -HUP) 某個服務!必須獨立成行 if [ -f /var/log/nginx/nginx.pid ]; then kill -USR1 `cat /var/log/nginx/nginx.pid` fi endscript } -
強制運行一次來測驗:
logrotate -f -v /etc/logrotate.d/nginx(對應目錄只能 user 有w權限, 否則會報錯) -
配置好即可, logrotate 會自動讀取
/etc/logrotate.d的配置并自動執行.
?? 備注:
cron.daily 會在
3:22+(5,45)這個時間段執行
4.2 NGINX + Keepalived配置
?? 假定:
假設相關IP如下:
- VIP: 192.168.0.100
- NGINX - 主IP: 192.168.0.1
- NGINX - 從IP: 192.168.0.2
- NGINX - 主從的IP對應的網卡為
eth0
4.2.1 Keepalived 安裝配置
$ sudo zypper install keepalived=2.0.10
4.2.2 Keepalived 配置
Keepalived啟動默認讀取組態檔路徑 /etc/keepalived/keepalived.conf ,添加組態檔:
sudo vi /etc/keepalived/keepalived.conf
NGINX - 主 機器配置如下:
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_pid.sh" # 檢測nginx狀態的腳本路徑
interval 2
weight -20
fall 3
}
vrrp_instance VI_1 {
state MASTER # 從機為 BACKUP
interface eth0 # 具體的網卡介面看情況進行填寫
virtual_router_id 51
priority 110 # 備機權值為100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.0.100/24 brd 192.168.0.255 dev eth0 label eth0:vip # 根據實際情況進行修改
}
}
配置說明如下:
-
weight -20: keepalived 配置里priority 110是初始權重;主的初始設定了 110,備的設定了 100;如果檢測失敗,權重 -20. 主從切換. -
virtual_router_id 51自己定義的虛擬路由的id,vrrp_instance VI_1塊中virtual_router_id指令的值 51 是一個示例值;根據需要改變它,使其在您的環境中獨一無二, -
添加
NGINX - 從 機器配置如下:
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_pid.sh" # 檢測nginx狀態的腳本路徑
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP # 輔機為 BACKUP
interface eth0 # 具體的網卡介面看情況進行填寫
virtual_router_id 51
priority 100 # 備機權值為100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.0.100/24 brd 192.168.0.255 dev eth0 label eth0:vip # 根據實際情況進行修改
}
}
4.2.3 配置檢測腳本
? 備注:
使用
root用戶
配置檢測腳本: /etc/keepalived/nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header|wc -l`
if [ $A -eq 0 ]
then
systemctl start nginx
exit 1
else
exit 0
fi
賦予執行權限: chmod 750 /etc/keepalived/nginx_pid.sh
4.2.4 啟動 Keepalived
鍵入以下 systemctl 命令以在系統啟動時啟用 keepalived: (開機自啟)
$ sudo systemctl enable keepalived
啟動 Nginx web 服務器:
$ sudo systemctl start keepalived
驗證:
$ systemctl status keepalived
4.2.4 顯示節點狀態
要查看當前哪個節點是給定 VIP 的主節點,請在定義 VRRP 實體的介面上運行命令 ip addr show(在以下命令中,eth0節點suse12-1和上的介面suse12-2):
suse12-1 # ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state
UP qlen 1000
link/ether 52:54:00:33:a5:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.1/24 brd 192.168.0.255 scope global dynamic eth0
valid_lft 3071sec preferred_lft 3071sec
inet 192.168.0.100/32 scope global eth0
valid_lft forever preferred_lft forever
suse12-2 # ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state
UP qlen 1000
link/ether 52:54:00:33:a5:87 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.2/24 brd 192.168.0.255 scope global eth0
valid_lft forever preferred_lft forever
在此輸出中,第二行 inet 表示 suse12-1 是主節點, 已為其分配了已定義的 VIP(192.168.0.100),inet 輸出中的其他行顯示主節點的實際IP地址(192.168.0.1)和備用(或 從)節點的IP地址(192.168.0.2),
至此, nginx + keepalived 的負載均衡層高可用已經配置完成.
4.3 NGINX -> 應用服務層 轉發配置
?? 假定:
假設應用系統的相關資訊如下:
- 請求及業務協議為HTTP協議;
- 應用系統節點1 IP為: 172.30.0.1
- 應用系統節點2 IP為: 172.30.0.2
- 業務系統監聽埠為: 8080
4.3.1 修改組態檔
以 mes 系統高可用配置為例(協議為 HTTP 協議), 新增 /etc/nginx/conf.d/mes.conf:
# mes access
upstream mes{
# ip_hash; # 源地址會話保持, 按需開啟
server 172.30.0.1:8080;
server 172.30.0.2:8080;
}
server {
listen 80;
#server_name mes.example.com 192.168.0.100; # server_name為對應的訪問域名, 按需開啟
#set max uploading file size to 20m
client_max_body_size 20m;
#charset koi8-r;
# keepalive
# 設定代理的HTTP協議版本(默認是1.0版本)
proxy_http_version 1.1; # 按需調整
# 允許重新定義或追加欄位到傳遞給代理服務器的請求頭資訊(默認是close)
proxy_set_header Connection "";
proxy_set_header HOST $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log logs/mes_access.log main buffer=16k flush=5m;
error_log logs/mes_error.log;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://mes;
#proxy_redirect off;
# proxy_cache cache_all;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#}
}
4.3.2 主組態檔 include
在 http 塊新增如下內容
include conf.d/mes.conf;
4.3.2 重新加載 nginx
更改配置后重新加載Ngnix使配置生效:
sudo systemctl reload nginx
4.3.3 驗證
通過 VIP 進行訪問, 查看對應應用系統日志, 觀察是否 2 個節點都接收到請求.
五 變更控制流程
? 嚴格遵循 制造業本公司的變更控制規范. 進行相關的變更控制和審批流程.
一個新的應用需要上線或者變更,通常涉及以下步驟:
可以隨時實施的變更:
- 系統專案組需至少提供埠、路徑、后端地址(域名可選);
- 在
/etc/nginx/conf.d中增加一個組態檔; - 在
/etc/nginx/nginx.conf中增加一個include,指向新增的組態檔;
? 需要在變更視窗進行的變更:
- 重新載入(
sudo systemctl reload nginx)配置. - 通知用戶, 訪問的 Server IP 由: 原應用服務器 IP 調整為 NGINX 的 VIP.
- 修改客戶端和瀏覽器配置, 訪問的 Server IP 由: 原應用服務器 IP 調整為 NGINX 的 VIP.
- 驗證變更是否成功.
5.1 回退步驟
回退較為簡單, 步驟如下:
- 通知用戶, 訪問的 Server IP 調整為: 原應用服務器 IP.
- 修改客戶端和瀏覽器配置, 訪問的 Server IP 調整為: 原應用服務器 IP.
- 回退完成
- 驗證回退是否成功.
參考檔案
| 參考檔案 |
|---|
| High Availability Support for NGINX |
三人行, 必有我師; 知識共享, 天下為公. 本文由東風微鳴技術博客 EWhisper.cn 撰寫.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/540294.html
標籤:其他
