upstream hello {
server 100.14.1.3:1201;
server 100.14.1.4:1201;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD /product HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name hello.youbest.com;
access_log /data/logs/nginx/hello.youbest.com.access.log main;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://hello;
}
}
首先要確認(rèn)服務(wù)類型是 http 還是tcp
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
or
check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
然后確認(rèn)健康檢查的url是否支持 HEAD請(qǐng)求或者GET 請(qǐng)求
[root@100-14-1-3 ~]# curl -v -XHEAD http://hello.youbest.com/product
HEAD / HTTP/1.1
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh3/1.4.2
Host: baidu.com
Accept: /< HTTP/1.1 200 OK
< Date: Tue, 07 Jan 2020 08:29:28 GMT
< Server: Apache
< Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
< ETag: "51-47cf7e6ee8400"
< Accept-Ranges: bytes
< Content-Length: 81
< Cache-Control: max-age=86400
< Expires: Wed, 08 Jan 2020 08:29:28 GMT
< Connection: Keep-Alive
< Content-Type: text/html
<
以上返回表示支持 HEAD 請(qǐng)求 nginx 就可以這樣做健康檢查
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD /product HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
其中 /product 表示服務(wù)里面的埋點(diǎn)路徑
< HTTP/1.1 200 OK 表示返回200狀態(tài)
如果HEAD 請(qǐng)求不支持 參考下面baidu.com的GET 請(qǐng)求
[root@vhs100-14-1-3 ~]# curl -v -XGET http://baidu.com
GET / HTTP/1.1
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh3/1.4.2
Host: baidu.com
Accept: /< HTTP/1.1 200 OK
< Date: Tue, 07 Jan 2020 08:41:38 GMT
< Server: Apache
< Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
< ETag: "51-47cf7e6ee8400"
< Accept-Ranges: bytes
< Content-Length: 81
< Cache-Control: max-age=86400
< Expires: Wed, 08 Jan 2020 08:41:38 GMT
< Connection: Keep-Alive
< Content-Type: text/html
<
以上表示支持 GET 請(qǐng)求 nginx 就可以這樣做健康檢查
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "GET /product HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
加了健康檢查后,當(dāng)后端有多臺(tái)服務(wù)器提供服務(wù)時(shí)
Down掉的機(jī)器就不會(huì)被代理轉(zhuǎn)發(fā)業(yè)務(wù)過去
從而保障業(yè)務(wù)的正常處理
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。