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

เซิร์ฟเวอร์ต้นทางและการกระจายโหลด

ตั้งค่าบล็อกเซิร์ฟเวอร์ต้นทางของ Nginx เพื่อจัดการเซิร์ฟเวอร์ปลายทางหลายเครื่องและกระจายคำขอที่เข้ามา

เซิร์ฟเวอร์ต้นทางและการกระจายโหลด เป็นบทเรียน 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 บทเรียน

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

Scaling Your Applications

As your web application grows, a single server might not be enough to handle all incoming requests. This can lead to slow response times or even crashes.

To prevent this, we use multiple backend servers to share the load. This improves both performance and reliability.

Introducing Upstream Servers

Nginx can act as a reverse proxy, distributing requests to a group of backend servers. These backend servers are often called upstream servers.

  • Scalability: Handle more traffic by adding more servers.
  • High Availability: If one server fails, others can still respond.
  • Load Balancing: Distribute requests efficiently among servers.

The `upstream` Block

In Nginx, you define a group of backend servers using the upstream block. This block gives a name to your group of servers, which you can then reference in your location blocks.

Think of it as a named pool of resources that Nginx can forward requests to.

Basic Upstream Configuration

Here's how to define a simple upstream block named my_backends with two servers. Each server directive specifies the address (IP or hostname) and port of a backend server.

upstream my_backends {
  server 192.168.1.100:8080;
  server 192.168.1.101:8080;
}

Connecting Upstream to `location`

Once an upstream block is defined, you can use it within a location block in your Nginx server configuration. The proxy_pass directive tells Nginx to forward requests to the specified upstream group.

Notice we use http://my_backends to reference our defined group.

http {
  upstream my_backends {
    server 192.168.1.100:8080;
    server 192.168.1.101:8080;
  }

  server {
    listen 80;
    location /app {
      proxy_pass http://my_backends;
    }
  }
}

Default Load Balancing: Round Robin

By default, Nginx uses the Round Robin load balancing method. This means requests are distributed to upstream servers in a rotating, sequential manner.

  • Request 1 goes to Server A.
  • Request 2 goes to Server B.
  • Request 3 goes to Server A.
  • ...and so on.

It's simple and effective for evenly distributed loads.

Customizing Load with `weight`

What if some servers are more powerful or should handle more traffic? You can use the weight parameter in the server directive to assign different priorities.

A server with a higher weight will receive a proportionally larger share of requests.

`weight` Example in Nginx

In this example, server 1 has a weight of 3, and server 2 has a weight of 1. This means server 1 will receive 3 times as many requests as server 2.

Out of every 4 requests, 3 will go to server 1 and 1 to server 2.

upstream my_backends {
  server 192.168.1.100:8080 weight=3;
  server 192.168.1.101:8080 weight=1;
}

Other Server Directives

Beyond weight, other directives can fine-tune server behavior:

  • max_fails=N: Number of failed attempts before Nginx considers the server unavailable.
  • fail_timeout=Xs: Time for which the server is considered unavailable after max_fails attempts.
  • backup: Marks a server as a backup, used only when all primary servers are down.
  • down: Marks a server as permanently unavailable.

These help Nginx manage server health automatically.

Quick Check: Upstream & Load Balancing

Consider the following Nginx configuration snippet:

upstream api_servers {
  server backend1.example.com:8080 weight=2;
  server backend2.example.com:8080;
}

server {
  listen 80;
  location /api {
    proxy_pass http://api_servers;
  }
}

If 9 requests arrive at the /api endpoint, how many requests will be sent to backend1.example.com:8080?

Recap & Next Steps

You've learned how Nginx uses upstream blocks to group multiple backend servers, enabling load balancing and improving application resilience.

  • upstream block: Defines a named group of servers.
  • proxy_pass: Connects a location to an upstream group.
  • Round Robin: Nginx's default load balancing method.
  • weight directive: Customizes request distribution among servers.

Next, we'll explore how Nginx can improve performance with proxy buffering and caching!

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

บทเรียน “เซิร์ฟเวอร์ต้นทางและการกระจายโหลด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เซิร์ฟเวอร์ต้นทางและการกระจายโหลด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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. การส่งต่อส่วนหัวและ IP ของไคลเอนต์
← กลับไปที่ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)