SJSJ88

🔒 Security Headers Setup

วิธีตั้ง Security Headers บน sj88ai.com ให้ทำงานผ่าน Cloudflare

⚠️ ปัญหา: nginx add_header ไม่ทำงานเมื่อผ่าน Cloudflare
Cloudflare strip headers ที่ nginx เพิ่มเข้าไปในการตอบกลับเมื่อ cf-cache-status: DYNAMIC หรือแม้แต่ HIT/MISS ทำให้ HSTS, CSP, X-Frame-Options ฯลฯ ที่ตั้งใน nginx ไม่ปรากฏ ใน HTTPS response

📊 สถานะ Headers ปัจจุบัน

Headerใน nginx configส่งออกมาจริง (HTTPS)
Strict-Transport-Security (HSTS)✗ (CF stripped)
X-Content-Type-Options✗ (CF stripped)
X-Frame-Options✗ (CF stripped)
Referrer-Policy✗ (CF stripped)
Permissions-Policy✗ (CF stripped)
Cross-Origin-Opener-Policy✗ (CF stripped)
Content-Security-Policy✗ (CF stripped)

✅ วิธีแก้: ใช้ Cloudflare Transform Rules

Cloudflare Dashboard → RulesTransform RulesModify Response Header

Step 1: เปิด Transform Rules

ไปที่: https://dash.cloudflare.com

  1. เลือกโดเมน sj88ai.com
  2. เมนูซ้าย: RulesTransform Rules
  3. แท็บ Modify Response Header
  4. กด Create rule

Step 2: ตั้ง Rule: HSTS

Rule name:    Security Headers (HSTS)
Match:        All incoming requests
Action:       Set new header
  Header name:  Strict-Transport-Security
  Value:        max-age=31536000; includeSubDomains; preload

Step 3: ตั้ง Rule รวม (1 rule ทำหลาย header ได้)

สร้าง rule เดียวแล้วเพิ่ม action หลายๆ อันได้:

Rule name:    Add Security Headers
Match:        All incoming requests

Actions (in order):
1. Set Strict-Transport-Security
     → max-age=31536000; includeSubDomains; preload

2. Set X-Content-Type-Options
     → nosniff

3. Set X-Frame-Options
     → SAMEORIGIN

4. Set Referrer-Policy
     → strict-origin-when-cross-origin

5. Set Permissions-Policy
     → camera=(), microphone=(), geolocation=(), interest-cohort=()

6. Set Cross-Origin-Opener-Policy
     → same-origin

7. Set Content-Security-Policy
     → default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' cdn.jsdelivr.net unpkg.com cdnjs.cloudflare.com esm.sh; style-src 'self' 'unsafe-inline' fonts.googleapis.com; font-src 'self' fonts.gstatic.com; img-src 'self' data: blob: https:; connect-src 'self'; frame-src 'self' https://www.youtube.com https://www.youtube-nocookie.com; worker-src 'self' blob:;

Step 4: Deploy

  1. กด Deploy
  2. รอ ~30 วินาที ให้ CF propagate
  3. ตรวจสอบด้วย: curl -I https://sj88ai.com/

🔍 ตรวจสอบหลัง Deploy

ใช้ Security Headers checker:

📋 Alternative: ปิด Cloudflare Proxy (DNS Only)

ถ้าอยากให้ nginx add_header ทำงานโดยตรง:

  1. Cloudflare DNS → ปิด orange cloud (Proxied → DNS only)
  2. ตอนนี้ traffic จะวิ่งตรงจากผู้ใช้ → VPS (ไม่ผ่าน CF proxy)
  3. nginx headers จะปรากฏ
ข้อเสีย: เสีย DDoS protection, CDN cache, WAF ของ Cloudflare

🛠 Alternative: ใช้ Workers

เขียน Cloudflare Worker เพื่อเพิ่ม headers แบบ programmatic:

export default {
  async fetch(request) {
    const response = await fetch(request);
    const newHeaders = new Headers(response.headers);
    newHeaders.set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
    newHeaders.set('X-Content-Type-Options', 'nosniff');
    newHeaders.set('X-Frame-Options', 'SAMEORIGIN');
    newHeaders.set('Referrer-Policy', 'strict-origin-when-cross-origin');
    return new Response(response.body, {
      status: response.status,
      statusText: response.statusText,
      headers: newHeaders
    });
  }
};

✅ สรุป: ทางเลือกที่แนะนำ

วิธีข้อดีข้อเสีย
Transform Rules (แนะนำ)ตรงไปตรงมา, ไม่กระทบ performance, ไม่เสีย CF featuresต้อง manual click ใน CF Dashboard
DNS Only (ปิด proxy)nginx headers ทำงานทันทีเสีย DDoS + CDN + WAF
Workerprogrammatic, ยืดหยุ่นWorkers plan ต้องจ่ายเงิน (Free tier มี 100K req/day)

🔄 Last updated: 2026-07-12 · กลับ Docs