API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · บทเรียน

การจำกัดอัตราและการควบคุมปริมาณด้วย Nginx

ตั้งค่าการจำกัดอัตราเพื่อป้องกันบริการปลายทางจากการใช้งานในทางที่ผิด และรับรองการใช้ทรัพยากรอย่างเป็นธรรม

บทเรียน 3 จาก 411 ขั้นตอน

การจำกัดอัตราและการควบคุมปริมาณด้วย Nginx เป็นบทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Rate Limiting Matters

Imagine a popular website or API. What happens if one user or a malicious bot sends thousands of requests per second?

This is where rate limiting comes in! It's a crucial technique to control the number of requests a client can make to your server within a specific timeframe.

  • Prevents abuse and DDoS attacks.
  • Ensures fair resource usage for all clients.
  • Protects your backend services from overload.

Nginx's Key Directives

Nginx provides powerful directives to implement rate limiting. We'll focus on two main ones:

  • limit_req_zone: Defines the parameters for a rate limiting zone. Think of it as setting up the rules for a specific type of traffic.
  • limit_req: Applies the defined rate limiting rules to requests within a specific location or server block. This is where the magic happens!

Defining Your Rate Limit Zone

The limit_req_zone directive is typically placed in the http block of your Nginx configuration. It defines a shared memory zone where Nginx keeps track of request states.

Here's its structure and what each part means:

  • key: What Nginx tracks (e.g., $binary_remote_addr for client IP).
  • zone: A name for your zone and its size (e.g., my_limit:10m). The size determines how many unique keys Nginx can track.
  • rate: The actual rate limit (e.g., rate=1r/s for 1 request per second).

Setting Up Your First Zone

Let's define a simple rate limiting zone that tracks requests by client IP address and allows 5 requests per second.

http {
    # ... other http settings ...

    limit_req_zone $binary_remote_addr zone=my_ip_limit:10m rate=5r/s;

    server {
        # ...
    }
}

Attaching Limits to Locations

After defining a limit_req_zone, you need to apply it to specific parts of your website or API using the limit_req directive.

This directive is placed inside a server or location block. It simply references the zone you created earlier.

For example, to apply the my_ip_limit zone to a specific location:

limit_req zone=my_ip_limit;

When a client exceeds the defined rate, Nginx will return a 503 Service Unavailable error by default.

A Complete Basic Rate Limit

Here's how you can combine both directives to limit requests to your /api/ endpoint to 2 requests per second per unique IP address.

http {
    limit_req_zone $binary_remote_addr zone=api_requests:10m rate=2r/s;

    server {
        listen 80;
        server_name example.com;

        location /api/ {
            limit_req zone=api_requests;
            proxy_pass http://backend_service;
        }
    }
}

Allowing Temporary Spikes

Strict rate limits can sometimes be too restrictive for legitimate users. The burst parameter allows a client to make requests exceeding the defined rate temporarily.

  • burst=N: Allows requests up to N more than the rate limit. These requests are queued and processed at the rate limit.
  • nodelay: When used with burst, Nginx processes burst requests immediately if possible. If the queue is full, subsequent requests are dropped (503 error) instead of being delayed.

Without nodelay, requests exceeding the rate will be delayed to conform to the rate.

Rate Limiting with Burst Tolerance

Let's update our previous example to allow a burst of up to 5 additional requests, processing them immediately if resources permit.

http {
    limit_req_zone $binary_remote_addr zone=api_requests:10m rate=2r/s;

    server {
        listen 80;
        server_name example.com;

        location /api/ {
            limit_req zone=api_requests burst=5 nodelay;
            proxy_pass http://backend_service;
        }
    }
}

Rate Limiting vs. Throttling

While often used interchangeably, there's a subtle difference:

  • Rate Limiting: Enforces a hard limit on the number of requests over a period (e.g., 100 requests per minute). Nginx's limit_req primarily implements rate limiting.
  • Throttling: Is a more dynamic process that might slow down requests rather than outright rejecting them. It often considers server load or resource availability. While Nginx can delay requests with burst (if nodelay is absent), it's more focused on strict limits.

For most API protection needs, Nginx's rate limiting capabilities are robust and highly effective.

Quick Check

You've learned about the core Nginx directives for rate limiting. Now, let's test your knowledge!

Summary: Protecting Your APIs

In this lesson, you've learned how to implement rate limiting with Nginx to protect your backend services and ensure fair usage.

  • We defined a rate limiting zone using limit_req_zone.
  • We applied these limits to specific locations using limit_req.
  • We explored the burst and nodelay options to handle temporary traffic spikes more gracefully.

Rate limiting is a fundamental security and performance pattern, especially when dealing with public APIs or high-traffic web applications.

เริ่มต้นได้ฟรี

เรียนรู้ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การจำกัดอัตราและการควบคุมปริมาณด้วย Nginx” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจำกัดอัตราและการควบคุมปริมาณด้วย Nginx” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจำกัดอัตราและการควบคุมปริมาณด้วย Nginx”

ตั้งค่าการจำกัดอัตราเพื่อป้องกันบริการปลายทางจากการใช้งานในทางที่ผิด และรับรองการใช้ทรัพยากรอย่างเป็นธรรม คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การจำกัดอัตราและการควบคุมปริมาณด้วย Nginx” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) นี้ได้ไหม

ได้ บทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การกำหนดเวอร์ชัน API ด้วย Nginx
  2. การแชร์ทรัพยากรข้ามต้นทาง (CORS)
  3. การจำกัดอัตราและการควบคุมปริมาณด้วย Nginx
  4. การกำหนดเส้นทางไปยังไมโครเซอร์วิสตามพาธ
← กลับไปที่ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)