0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · レッスン

ヘルスチェックとサーバー監視

ヘルスチェックを実装し、正常でないバックエンドサーバーをロードバランシング対象から自動的に除外します。

「ヘルスチェックとサーバー監視」はCoddyKit上の無料API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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_timeout expires, 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_fails and fail_timeout based 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.

よくある質問

「ヘルスチェックとサーバー監視」レッスンは無料ですか?

はい。「ヘルスチェックとサーバー監視」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、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)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)を始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「ヘルスチェックとサーバー監視」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンでコードを書いて実行できますか?

はい。すべてのAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ロードバランシングアルゴリズム
  2. ヘルスチェックとサーバー監視
  3. スティッキーセッションとセッション永続化
  4. 重み付けロードバランシングとバックアップサーバー
← API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)に戻る