Security Headers เป็นส่วนหนึ่งของ HTTP Response ที่ช่วยป้องกันการโจมตีหลายประเภทบนเว็บไซต์ เช่น Clickjacking, XSS, และ MIME-type Sniffing Cloudflare ช่วยให้คุณเพิ่ม Header เหล่านี้ได้ง่ายผ่าน Transform Rules โดยไม่ต้องแก้ไขอะไรบน Origin Server จึงเหมาะสำหรับผู้ใช้ VPS
Security Headers ที่สำคัญที่สุด
1. Strict-Transport-Security (HSTS)
บังคับให้เบราว์เซอร์ใช้เฉพาะ HTTPS ในการเชื่อมต่อด้วน Cloudflare มีตัวเลือกเปิด HSTS โดยตรงใน SSL/TLS Settings
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
2. X-Frame-Options
ป้องกัน Clickjacking โดยไม่ให้เว็บอื่น Embed เว็บของคุณใน iframe
X-Frame-Options: SAMEORIGIN
3. X-Content-Type-Options
ป้องกันเบราว์เซอร์สุ่ม MIME Type ของไฟล์ ซึ่งอาจนำไปสู่การรัน Script ที่ไม่ควรรัน
X-Content-Type-Options: nosniff
4. Referrer-Policy
ควบคุมว่าข้อมูล URL ใดต้องส่งไปใน Referrer Header เมื่อคลิกเย้าเว็บอื่น
Referrer-Policy: strict-origin-when-cross-origin
5. Permissions-Policy
ควบคุมการใช้งาน Browser Features เช่น Camera, Microphone, Geolocation
Permissions-Policy: camera=(), microphone=(), geolocation=()
วิธีเพิ่ม Security Headers ผ่าน Cloudflare Transform Rules
- ไปที่ Rules → Transform Rules
- เลือก Modify Response Header
- คลิก Create Rule
- ตั้งชื่อ Rule และกำหนด Filter (All incoming requests)
- ในส่วน Then… เลือก Set static และใส่ชื่อ Header + ค่า
- เพิ่มได้หลาย Header ใน Rule เดียวกัน
- คลิก Deploy
ตัวอย่าง Security Headers ชุดแนะนำ
| Header | ค่าที่แนะนำ | ป้องกัน |
|---|---|---|
| X-Frame-Options | SAMEORIGIN | Clickjacking |
| X-Content-Type-Options | nosniff | MIME Sniffing |
| Referrer-Policy | strict-origin-when-cross-origin | Information Leakage |
| Permissions-Policy | camera=(), microphone=() | Feature Abuse |
| X-XSS-Protection | 1; mode=block | XSS (เบราว์เซอร์เก่า) |
ตรวจสอบ Security Headers
หลังตั้งค่าแล้ว สามารถตรวจสอบได้ด้วย:
- securityheaders.com: กรอก URL เพื่อตรวจสอบและรับคะแนน A ถึง F
- curl:
curl -I https://yourdomain.comแล้วดูใน Response Header - Chrome DevTools: เปิด Network Tab แล้วคลิกที่ Request ก็เห็น Response Header
สรุป
Security Headers เป็นสิ่งที่เว็บหลายแห่งมักหลงลืม Cloudflare ให้คุณเพิ่มได้ง่ายผ่าน Transform Rules โดยไม่ต้องแตะต้องไปสัมผัส Origin Server เหมาะสำหรับเว็บบน VPS ที่ต้องการความปลอดภัยครบถ้วน

