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

การบัฟเฟอร์และการแคชของพร็อกซี

สำรวจกลไกการบัฟเฟอร์และการแคชของพร็อกซีใน 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 บทเรียน

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

Boosting App Performance

In web development, speed is crucial! Users expect fast loading times, and search engines reward performant sites.

Nginx, acting as a reverse proxy, can significantly enhance your application's speed and reliability.

Today, we'll explore two powerful Nginx features: buffering and caching.

Understanding Proxy Buffering

Proxy buffering is when Nginx temporarily stores responses from your backend server before sending them to the client.

Think of it like a waiting room for data. Instead of sending data directly as it arrives, Nginx collects the full response (or parts of it) first.

This decouples the backend server from the client, improving efficiency.

How Buffering Works

Without buffering, Nginx acts as a simple relay. If the backend is slow, the client experiences that slowness directly.

With buffering, Nginx:

  • Receives the full response from the backend.
  • Stores it in its internal buffers (memory/disk).
  • Sends the response to the client at the client's speed.

This protects your backend from slow client connections.

Why Buffering Helps

Buffering offers several key advantages:

  • Frees Backend: Your backend server can finish sending its response quickly and serve other requests.
  • Handles Slow Clients: Nginx can serve content to slow clients without holding up the backend.
  • Improves Reliability: Prevents backend from being overwhelmed by network issues or slow connections.

Key Buffering Settings

Nginx uses specific directives to control buffering. Here are the main ones:

  • proxy_buffering on;: Enables (or disables) buffering. It's usually on by default.
  • proxy_buffers 4 8k;: Sets the number (4) and size (8KB) of buffers for a response.
  • proxy_buffer_size 4k;: Sets the size of the first buffer, typically for response headers.

These are set within your location or http blocks.

http {
    proxy_buffers 4 8k;
    proxy_buffer_size 4k;

    server {
        listen 80;
        location /app {
            proxy_pass http://backend_server;
            proxy_buffering on; # Explicitly enable
        }
    }
}

Simple Buffering Setup

Let's see a basic configuration to enable and configure buffering for a specific location:

This setup ensures Nginx buffers up to 32KB (4 buffers * 8KB each) of response data from the backend.

server {
    listen 80;
    server_name example.com;

    location /api/data {
        proxy_pass http://my_backend_service;
        proxy_buffering on;
        proxy_buffers 8 16k; # 8 buffers, each 16KB
        proxy_buffer_size 8k; # First buffer 8KB
        proxy_busy_buffers_size 16k; # Limit busy buffers
    }
}

Discovering Proxy Caching

While buffering deals with how Nginx handles a single ongoing response, proxy caching stores copies of responses for future requests.

If multiple clients request the same resource, Nginx can serve it directly from its cache instead of asking the backend repeatedly.

This is a powerful way to reduce load on your backend servers.

How Caching Boosts Speed

When a request comes in:

  1. Nginx checks its cache for a valid, stored response.
  2. If found, it serves it immediately (cache hit).
  3. If not, it forwards the request to the backend, receives the response, stores it in cache, and then serves it to the client (cache miss).

Benefits: Faster response times, reduced backend load, and improved scalability.

Essential Caching Settings

To set up caching, you need to define a cache zone and then apply it to your locations:

  • proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m inactive=60m max_size=1g;: Defines the cache storage.
  • proxy_cache my_cache;: Activates the named cache zone for a location.
  • proxy_cache_valid 200 302 10m;: Sets how long responses with specific status codes are considered valid in cache.
http {
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m
                     inactive=60m max_size=1g;

    server {
        listen 80;
        location /images/ {
            proxy_pass http://image_backend;
            proxy_cache my_cache;
            proxy_cache_valid 200 1d; # Cache 200 OK for 1 day
        }
    }
}

Basic Caching Setup

Here's how to configure Nginx to cache static assets like images, CSS, or JavaScript files.

This example sets up a cache named 'static_cache' and uses it for requests to /assets/ paths, caching successful responses for 60 minutes.

http {
    # Define a cache zone
    proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=static_cache:20m
                     inactive=60m max_size=5g;

    server {
        listen 80;
        server_name cdn.example.com;

        location /assets/ {
            proxy_pass http://asset_service;
            proxy_cache static_cache; # Use the defined cache
            proxy_cache_valid 200 60m; # Cache 200 OK for 60 mins
            proxy_cache_revalidate on; # Revalidate stale cache
            add_header X-Cache-Status $upstream_cache_status;
        }
    }
}

Test Your Knowledge

Let's check your understanding of Nginx's performance features.

Buffering & Caching Recap

Great job! You've learned how Nginx can significantly improve application performance and backend resilience.

  • Proxy buffering decouples backend and client, handling responses efficiently.
  • Proxy caching stores responses to serve repeated requests faster and reduce backend load.

Mastering these features is key to building high-performance web applications with 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 บทเรียน

บทเรียน “การบัฟเฟอร์และการแคชของพร็อกซี” ใช้เวลานานแค่ไหน

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

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

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

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

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