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

GatewayFilters ในตัวที่ควรรู้จัก

สำรวจโรงงาน GatewayFilter ที่พร้อมใช้งานและมีประโยชน์ที่สุด เพื่อแก้ปัญหาทั่วไปได้โดยไม่ต้องเขียนโค้ดแบบกำหนดเอง

บทเรียน 4 จาก 413 ขั้นตอน

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

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

Filters You Get for Free

Before writing custom filters, know that Spring Cloud Gateway ships with dozens of ready-made GatewayFilter factories. You attach them per route in configuration.

This lesson tours the ones you will reach for most often.

AddRequestHeader

AddRequestHeader injects a header into every request before it reaches the backend, useful for tagging traffic from the gateway.

filters:
  - AddRequestHeader=X-Source, gateway

AddResponseHeader

Likewise, AddResponseHeader adds a header to the response sent back to the client, for example a marker showing which gateway handled it.

filters:
  - AddResponseHeader=X-Handled-By, edge-gateway

RewritePath

RewritePath uses a regex to transform the incoming path before forwarding. Strip a public prefix while keeping the rest.

filters:
  - RewritePath=/api/(?<seg>.*), /$\{seg}

StripPrefix

When you just need to drop leading path segments, StripPrefix is simpler than a rewrite. StripPrefix=1 removes the first segment.

filters:
  - StripPrefix=1
# /service/users -> /users

PrefixPath

The reverse of stripping, PrefixPath prepends a segment to the path sent upstream.

filters:
  - PrefixPath=/internal
# /users -> /internal/users

SetStatus

SetStatus forces the HTTP status code of the response, handy for maintenance routes or deprecation notices.

filters:
  - SetStatus=410

RedirectTo

RedirectTo returns a redirect response with a status and target URL instead of proxying.

filters:
  - RedirectTo=302, https://new.example.com

RemoveRequestHeader

Strip a header before it reaches the backend with RemoveRequestHeader, for example to drop a client-supplied header you do not trust.

filters:
  - RemoveRequestHeader=Cookie

A Full Route Example

Filters stack in declared order. Here a route strips a prefix, tags the request, and marks the response.

routes:
  - id: users
    uri: http://users-svc:8080
    predicates:
      - Path=/api/users/**
    filters:
      - StripPrefix=2
      - AddRequestHeader=X-Source, gateway
      - AddResponseHeader=X-Handled-By, edge

Default Filters

Use default-filters to apply a filter to every route at once, such as adding a correlation header everywhere.

spring:
  cloud:
    gateway:
      default-filters:
        - AddResponseHeader=X-Gateway, true

Quick Check

You want to turn /api/users into /users by dropping the first path segment. Which filter is the simplest fit?

Recap

You now have a toolbox of built-in filters:

  • Header filters: Add/Remove request and response headers
  • Path filters: StripPrefix, PrefixPath, RewritePath
  • Response control: SetStatus, RedirectTo
  • default-filters apply globally

Reach for these before writing custom code, then build custom filters only when nothing fits.

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

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

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

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

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

บทเรียน “GatewayFilters ในตัวที่ควรรู้จัก” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “GatewayFilters ในตัวที่ควรรู้จัก”

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

บทเรียน “GatewayFilters ในตัวที่ควรรู้จัก” ใช้เวลานานแค่ไหน

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

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

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

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

  1. ตัวกรองส่วนกลางและ GatewayFilterFactory
  2. ตัวกรองคำขอและการตอบกลับแบบกำหนดเอง
  3. การประมวลผลก่อนและหลังด้วยตัวกรอง
  4. GatewayFilters ในตัวที่ควรรู้จัก
← กลับไปที่ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)