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

เสริมความปลอดภัยให้ Nginx ด้วยส่วนหัวความปลอดภัย

เพิ่มส่วนหัวความปลอดภัย HTTP ใน Nginx เพื่อป้องกันการโจมตีแบบคลิกแจ็กกิง การตรวจเดาชนิด MIME และการแทรกเนื้อหา

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

เสริมความปลอดภัยให้ Nginx ด้วยส่วนหัวความปลอดภัย เป็นบทเรียน 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 บทเรียน

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

Headers as a Defense Layer

Beyond TLS and authentication, modern browsers honor security headers that instruct them how to behave. Nginx can inject these on every response with the add_header directive.

Preventing MIME Sniffing

X-Content-Type-Options: nosniff stops browsers from guessing a resource's type, blocking attacks that disguise a script as an image.

add_header X-Content-Type-Options "nosniff" always;

Blocking Clickjacking

X-Frame-Options controls whether your site can be embedded in a frame. Use DENY or SAMEORIGIN to prevent clickjacking.

add_header X-Frame-Options "SAMEORIGIN" always;

Strict Transport Security

HSTS forces browsers to use HTTPS for future visits. Set a long max-age once HTTPS is stable everywhere.

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

Content Security Policy

A Content-Security-Policy restricts where scripts, styles, and other resources may load from, mitigating cross-site scripting.

add_header Content-Security-Policy "default-src 'self'" always;

Controlling the Referrer

Referrer-Policy limits how much referrer information leaks to other sites when users click outbound links.

add_header Referrer-Policy "strict-origin-when-cross-origin" always;

Why the always Flag

Without always, Nginx adds the header only on successful responses (2xx, 3xx). The always flag ensures the header is present on error responses too.

add_header X-Frame-Options "DENY" always;

Hiding the Nginx Version

By default Nginx reveals its version in the Server header and error pages. Turn this off to give attackers less information.

server_tokens off;

The add_header Inheritance Trap

If a location block has its own add_header, it replaces all inherited headers from the parent. Re-declare needed headers in nested blocks.

# headers in http/server are dropped here
location /api {
    add_header X-Content-Type-Options "nosniff" always;
}

Grouping Security Headers

Keep all security headers in one include file and pull it into each server block for consistency.

# security_headers.conf
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;

# server block:
include /etc/nginx/security_headers.conf;

Verifying Headers

Use curl to inspect the response headers and confirm each one is present, even on error responses.

curl -I https://example.com

Quick Check

Which header tells the browser to refuse loading your site inside a frame on another domain?

Recap

You hardened Nginx with browser security headers:

  • nosniff blocks MIME confusion
  • X-Frame-Options stops clickjacking
  • HSTS enforces HTTPS, CSP restricts resources
  • Use always and beware add_header inheritance
  • server_tokens off hides the version

These complement TLS and authentication for defense in depth.

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

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

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

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

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

บทเรียน “เสริมความปลอดภัยให้ Nginx ด้วยส่วนหัวความปลอดภัย” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “เสริมความปลอดภัยให้ Nginx ด้วยส่วนหัวความปลอดภัย”

เพิ่มส่วนหัวความปลอดภัย HTTP ใน Nginx เพื่อป้องกันการโจมตีแบบคลิกแจ็กกิง การตรวจเดาชนิด MIME และการแทรกเนื้อหา คุณปฏิบัติ 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 บทเรียน

บทเรียน “เสริมความปลอดภัยให้ Nginx ด้วยส่วนหัวความปลอดภัย” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การรักษาความปลอดภัย Nginx ด้วย SSL/TLS
  2. HTTP/2 และการเพิ่มประสิทธิภาพ Nginx
  3. การยืนยันตัวตนพื้นฐานและการควบคุมการเข้าถึง
  4. เสริมความปลอดภัยให้ Nginx ด้วยส่วนหัวความปลอดภัย
← กลับไปที่ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)