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

การตั้งค่าพร็อกซีย้อนกลับแบบง่าย

ตั้งค่า Nginx ให้ส่งต่อคำขอไปยังแอปพลิเคชันหรือเซิร์ฟเวอร์ปลายทางเพียงหนึ่งรายการ

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

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

What's a Reverse Proxy?

In simple terms, a reverse proxy acts as an intermediary between clients (like your web browser) and your backend servers. When you send a request, it goes to the reverse proxy first.

The reverse proxy then forwards your request to the appropriate backend server, collects the response, and sends it back to you. It's like a helpful receptionist for your web services!

Nginx: Your Traffic Cop

Nginx (pronounced "engine-x") is a popular, high-performance web server that can also function as a very efficient reverse proxy.

It sits at the "edge" of your network, receiving all incoming client requests. Based on its configuration, Nginx decides which backend application or server should handle the request.

Think of it as a traffic cop, directing incoming requests to the correct destination.

Nginx Server Block Basics

Nginx configurations are organized into blocks. The primary block for handling incoming connections is the server block.

Each server block defines a virtual host, specifying:

  • The port Nginx listens on (listen directive).
  • The domain names it responds to (server_name directive).

It's where Nginx decides which specific application configuration to use for a given request.

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

    # Other configurations will go here
}

Routing with Location Blocks

Inside a server block, you use location blocks to define how Nginx should handle requests for specific URLs or URL patterns.

For example, a location / {} block matches all requests, while a location /api {} block matches requests starting with /api.

This allows you to route different parts of your application to different backend services.

server {
    listen 80;
    server_name myapp.com;

    location / {
        # How to handle requests to myapp.com/
    }

    location /api {
        # How to handle requests to myapp.com/api
    }
}

Forwarding Requests: proxy_pass

The magic directive for reverse proxying is proxy_pass. This directive tells Nginx where to forward the incoming request.

You place proxy_pass inside a location block. Its value is the URL of your backend server or application.

  • http://localhost:8080: A local web server.
  • http://my-backend-service.com: A remote service.

Nginx will take the client's request and send it directly to this specified URL.

Basic Reverse Proxy Setup

Let's create a simple Nginx configuration to proxy requests. We'll set Nginx to listen on port 80 and forward all requests to a backend application running on localhost:8080.

This hides the backend's port and allows Nginx to handle public traffic.

server {
    listen 80;
    server_name myapp.com;

    location / {
        proxy_pass http://localhost:8080;
    }
}

Preserving Request Info

When Nginx proxies a request, it acts on behalf of the client. By default, some original client information (like their IP address) might be lost or replaced by Nginx's own details.

To preserve this information, we use proxy_set_header directives. Key headers to set include:

  • Host: Original requested host.
  • X-Real-IP: The client's actual IP address.
  • X-Forwarded-For: A list of proxy IPs, including the client's.
  • X-Forwarded-Proto: The original protocol (HTTP/HTTPS).

These headers are crucial for backend applications to correctly identify the client and handle requests.

Proxying with Proper Headers

It's good practice to include essential headers in your reverse proxy configuration. This helps your backend application receive the correct client information.

Here's our previous example, updated with common proxy_set_header directives.

server {
    listen 80;
    server_name myapp.com;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Why Use a Simple Proxy?

Even a basic reverse proxy setup offers significant advantages:

  • Security: Hides the backend server's direct IP and port from the public.
  • Load Distribution: (Even for one server, it prepares for more).
  • SSL Termination: Nginx can handle HTTPS encryption, offloading the backend.
  • URL Rewriting: Present a cleaner URL to clients while internally routing to complex paths.

This single point of entry simplifies management and enhances control over traffic.

Quick Check: Nginx Proxy

Consider the following Nginx configuration snippet. Which directive is used to forward client requests to a backend server?

server {
listen 80;
server_name example.com;
location / {
# THIS IS WHERE THE DIRECTIVE GOES
http://my-backend-app:9000;
}
}

Summary: Simple Proxying

Great job! You've learned the fundamentals of configuring Nginx as a simple reverse proxy.

We covered:

  • The role of server and location blocks.
  • Using proxy_pass to forward requests.
  • The importance of proxy_set_header for preserving client information.

This foundational knowledge is key to building more complex and robust Nginx setups. Next, we'll explore how to manage multiple backend servers!

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

บทเรียน “การตั้งค่าพร็อกซีย้อนกลับแบบง่าย” ฟรีหรือไม่

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