การตรวจสอบสถานะและการเฝ้าติดตามเซิร์ฟเวอร์
ใช้งานการตรวจสอบสถานะเพื่อถอนเซิร์ฟเวอร์ปลายทางที่ไม่พร้อมใช้งานออกจากกลุ่มกระจายโหลดโดยอัตโนมัติ
การตรวจสอบสถานะและการเฝ้าติดตามเซิร์ฟเวอร์ เป็นบทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Keeping Servers Healthy
Imagine a busy restaurant with multiple chefs. If one chef gets sick, you don't want to send orders to them, right?
In load balancing, health checks do exactly that. They constantly monitor backend servers to ensure they are ready to handle requests.
Why Health Checks Matter
Without health checks, a load balancer might keep sending requests to a server that is down, overloaded, or unresponsive.
- Bad User Experience: Users get errors instead of content.
- Wasted Resources: The load balancer tries to connect to a dead end.
- Cascading Failures: Overloads can spread if requests aren't redirected.
Nginx's Health Check Basics
Nginx provides built-in directives within its upstream block to perform passive health checks. These checks react to failed connection attempts or responses.
The two main directives are max_fails and fail_timeout.
`max_fails`: Failed Attempts
The max_fails directive sets the number of consecutive failed attempts after which Nginx considers a server "unhealthy".
- Default:
1(one failure marks it unhealthy). - `max_fails=0`: Disables health checks for that server.
- A "failure" can be a connection error, timeout, or specific HTTP status codes (like 5xx) if configured.
`fail_timeout`: Recovery Time
Once a server is marked unhealthy, Nginx will stop sending requests to it for a duration specified by fail_timeout.
- Default:
10s(10 seconds). - After this timeout, Nginx will tentatively send a request to the server to check if it has recovered.
- This prevents hammering a down server.
Implementing Nginx Health Checks
Let's see how to configure max_fails and fail_timeout in an Nginx upstream block. Here, we define two backend servers.
http {
upstream backend_servers {
server 192.168.1.100:8080 max_fails=3 fail_timeout=15s;
server 192.168.1.101:8080 max_fails=3 fail_timeout=15s;
}
server {
listen 80;
location / {
proxy_pass http://backend_servers;
}
}
}Nginx's Server Management
When a server hits its max_fails limit within the fail_timeout period, Nginx temporarily removes it from the load balancing pool.
- Requests are then distributed among the remaining healthy servers.
- After
fail_timeoutexpires, Nginx tries sending a single request to the "unhealthy" server. If it succeeds, the server is marked healthy again.
Observing Server Health
While Nginx's basic health checks are passive, you can observe their effects in Nginx logs.
Error logs will show messages when a server is marked down or up. For more advanced, active monitoring and a dashboard, Nginx Plus offers dedicated features, but that's beyond basic Nginx.
Health Check Tips
Properly configuring health checks is key for reliable systems:
- Tune Values: Adjust
max_failsandfail_timeoutbased on your application's responsiveness and recovery time. - Backend Readiness: Ensure your backend applications have a dedicated health endpoint (e.g.,
/health) that reports true service readiness. - Combine with Monitoring: Use external monitoring tools to alert you when Nginx marks servers down.
Quick Check on Health Checks
You've configured an Nginx upstream server with max_fails=2 and fail_timeout=30s. If the server fails 3 consecutive requests, what happens next?
Health Check Recap
In this lesson, we learned about Nginx's essential health check directives:
max_fails: The number of failed attempts before a server is marked unhealthy.fail_timeout: The period for which an unhealthy server is taken out of the load balancing pool.- These passive checks are crucial for maintaining backend reliability and improving user experience.
คำถามที่พบบ่อย
บทเรียน “การตรวจสอบสถานะและการเฝ้าติดตามเซิร์ฟเวอร์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตรวจสอบสถานะและการเฝ้าติดตามเซิร์ฟเวอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตรวจสอบสถานะและการเฝ้าติดตามเซิร์ฟเวอร์”
ใช้งานการตรวจสอบสถานะเพื่อถอนเซิร์ฟเวอร์ปลายทางที่ไม่พร้อมใช้งานออกจากกลุ่มกระจายโหลดโดยอัตโนมัติ คุณปฏิบัติ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การตรวจสอบสถานะและการเฝ้าติดตามเซิร์ฟเวอร์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) นี้ได้ไหม
ได้ บทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- อัลกอริทึมการกระจายโหลด
- การตรวจสอบสถานะและการเฝ้าติดตามเซิร์ฟเวอร์
- เซสชันแบบยึดติดและการคงอยู่ของเซสชัน
- การกระจายโหลดตามน้ำหนักและเซิร์ฟเวอร์สำรอง