วิธีตั้ง Security Headers บน sj88ai.com ให้ทำงานผ่าน Cloudflare
add_header ไม่ทำงานเมื่อผ่าน Cloudflarecf-cache-status: DYNAMIC หรือแม้แต่ HIT/MISS
ทำให้ HSTS, CSP, X-Frame-Options ฯลฯ ที่ตั้งใน nginx ไม่ปรากฏ ใน HTTPS response
| 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 Dashboard → Rules → Transform Rules → Modify Response Header
ไปที่: https://dash.cloudflare.com
sj88ai.comRule name: Security Headers (HSTS)
Match: All incoming requests
Action: Set new header
Header name: Strict-Transport-Security
Value: max-age=31536000; includeSubDomains; preload
สร้าง 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:;
curl -I https://sj88ai.com/ใช้ Security Headers checker:
ถ้าอยากให้ nginx add_header ทำงานโดยตรง:
เขียน 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 |
| Worker | programmatic, ยืดหยุ่น | Workers plan ต้องจ่ายเงิน (Free tier มี 100K req/day) |
🔄 Last updated: 2026-07-12 · กลับ Docs