0Pricing
Django Academy · บทเรียน

Nginx ในฐานะพร็อกซีย้อนกลับ

วางไว้ด้านหน้า Gunicorn และให้บริการไฟล์สแตติก

Nginx ในฐานะพร็อกซีย้อนกลับ เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

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

Why Add Nginx

Gunicorn runs your code, but it should not face the raw internet alone. A reverse proxy sits in front and shields it. 🛡️

What a Reverse Proxy Does

A reverse proxy receives every browser request, then forwards it to Gunicorn and relays the answer back. The client never talks to Gunicorn directly.

Nginx Is Fast at Files

Nginx excels at serving static files and handling thousands of connections, freeing Gunicorn to focus only on dynamic Python work.

The Two-Layer Stack

The flow becomes browser to Nginx to Gunicorn to Django. Each layer does what it is best at, giving you speed and safety together.

A Server Block

Nginx config lives in a server block. It listens on a port and names the domain it answers for.

server {
    listen 80;
    server_name example.com;
}

Proxy to Gunicorn

Inside a location, proxy_pass tells Nginx where to send dynamic requests, usually Gunicorn on a local port or socket.

location / {
    proxy_pass http://127.0.0.1:8000;
}

Pass Real Headers

Add proxy_set_header lines so Django sees the true host and client IP, not just Nginx's local address.

proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;

Serve Static Directly

Map a location for /static/ to your collected files. Nginx serves them straight from disk, skipping Gunicorn entirely for speed.

location /static/ {
    alias /app/staticfiles/;
}

Where to Connect

Nginx can reach Gunicorn over a TCP port or a faster Unix socket. Sockets avoid network overhead on the same machine.

Test Before Reload

Always check your config with nginx -t before reloading. A bad file caught here saves you from taking the site offline.

nginx -t
systemctl reload nginx

TLS Lives Here Too

Nginx is also the natural place to terminate HTTPS, decrypting traffic once before passing plain requests on to Gunicorn.

Quick Check

Pick the directive that forwards requests to Gunicorn.

Recap

You set up Nginx as a reverse proxy: it fronts Gunicorn with proxy_pass, serves static files fast, forwards headers, and handles HTTPS. 🎯

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

บทเรียน “Nginx ในฐานะพร็อกซีย้อนกลับ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “Nginx ในฐานะพร็อกซีย้อนกลับ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “Nginx ในฐานะพร็อกซีย้อนกลับ”

วางไว้ด้านหน้า Gunicorn และให้บริการไฟล์สแตติก คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

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

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

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. Gunicorn ในฐานะเซิร์ฟเวอร์ WSGI
  2. Nginx ในฐานะพร็อกซีย้อนกลับ
  3. WhiteNoise สำหรับไฟล์สแตติก
  4. ตัวแปรสภาพแวดล้อมและการแยกการตั้งค่า
← กลับไปที่ Django Academy