How to prevent clickjacking with X-Frame-Options
Clickjacking is when another site loads yours in a hidden frame and tricks a visitor into clicking something on it, such as a confirm button, while they think they are clicking something harmless. The X-Frame-Options header, or the frame-ancestors directive of a Content-Security-Policy, tells the browser not to allow it.
Updated 2026-10-23
Why it matters
- Anything a logged-in visitor can do with a click, an attacker can try to trick them into doing.
- The fix is one header, and almost no site legitimately needs to be framed by strangers.
How to fix it
- Add this header to every response from example.com.
Pick what your site runs on. Replace example.com with your own domain where it appears.
After you deploy, re-check the site. A CDN or the browser cache can keep serving the old headers for a few minutes.
nginx
add_header X-Frame-Options "SAMEORIGIN" always;
Apache
Header always set X-Frame-Options "SAMEORIGIN"
Caddy
header {
X-Frame-Options "SAMEORIGIN"
}Cloudflare
X-Frame-Options: SAMEORIGIN
Vercel
{
"headers": [
{
"source": "/(.*)",
"headers": [
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN"
}
]
}
]
}Netlify
/* X-Frame-Options: SAMEORIGIN
Next.js
const nextConfig = {
async headers() {
return [
{
source: "/(.*)",
headers: [
{ key: "X-Frame-Options", value: "SAMEORIGIN" },
],
},
];
},
};
export default nextConfig;Express
app.use((req, res, next) => {
res.setHeader("X-Frame-Options", "SAMEORIGIN");
next();
});Common mistakes
- Using DENY when you frame your own pages (an admin preview, an embedded widget). Use SAMEORIGIN, which allows your own site.
- Using ALLOW-FROM. It is obsolete and ignored by modern browsers. Use frame-ancestors in a Content-Security-Policy if you must allow a specific other site.
- Setting it on the home page only. Set it on every response.
Check that it worked
Run your domain through the free security headers checker. It takes a few seconds, needs no account, and shows anything still missing.
Open the security headers checkerQuestions
- SAMEORIGIN or DENY?
- SAMEORIGIN unless you are sure nothing, including your own site, ever frames your pages. It protects against other sites while keeping your own features working.
- What if a partner needs to embed my page?
- Skip X-Frame-Options for that page and use a Content-Security-Policy with frame-ancestors listing exactly the sites you trust.
Related guides
Don't want to check by hand? Upzy watches your site around the clock and tells you when a header, certificate or email record slips. Start free.