How to add a Content-Security-Policy header
A Content-Security-Policy (CSP) header is a line your web server sends with each page, telling the browser which sources of scripts, images and frames it may load. To add it, set it in your server, CDN or hosting config, test it in report-only mode first, then enforce it.
A security scan may report a missing Content-Security-Policy header. This means your server is not telling browsers which outside sources your pages are allowed to load. Nothing looks broken to visitors, but one of your browser-side safety nets is switched off.
The fix is a single header, but a rushed one can break your site. This page explains how to add it safely.
What the problem is and what people see
Every page your site sends comes with hidden response headers. One of them can be Content-Security-Policy. It lists the places a page may load scripts, styles, images, fonts and frames from. Without it, the browser trusts anything the page asks for.
Visitors will not see anything different. You will usually find out from a security scan or a security review, where it appears as "Missing Content-Security-Policy header".
Why it matters
- Security: if an attacker manages to inject a script into one of your pages (cross-site scripting), a CSP can stop the browser running it or sending data to the attacker's server. This matters most on pages with logins, forms or payments.
- Trust and sales: security questionnaires from larger customers, and many audits, ask about security headers. A missing one is an easy item to flag.
- Search: Google has not said CSP is a ranking factor, so do not expect a jump in rankings. The benefit is protection, not SEO.
A CSP is a second line of defence. It does not replace keeping your software updated.
How to check if your site has it
- Browser tools: open your site, press F12, go to the
Networktab, reload, click the first request (your page address), and look underResponse Headersforcontent-security-policy. - Command line: run
curl -I https://yourdomain.comand look for acontent-security-policyline. - Free online checkers: securityheaders.com and the Mozilla Observatory both test your headers and show what is missing.
Check your main page and also a few inner pages, such as checkout or login, because headers can differ by path.
How to fix it in general
The safe way is to start in report-only mode. The browser then logs what the policy would have blocked, without blocking anything.
- List what your site loads from other places: analytics, tag managers, fonts, chat widgets, payment forms, maps, embedded videos.
- Add a policy using the header name
Content-Security-Policy-Report-Only. - Browse your key pages and forms with the browser console open (F12,
Console). Each message names something the policy would have blocked. - Add the legitimate sources to the policy and repeat until the console is quiet.
- Rename the header to
Content-Security-Policyso it is enforced.
A starting policy to adjust, not to copy blindly:
Content-Security-Policy-Report-Only: default-src 'self'; img-src 'self' data: https:; script-src 'self' https://www.googletagmanager.com; style-src 'self' 'unsafe-inline'; base-uri 'self'; form-action 'self'
default-src 'self' means everything must come from your own domain unless a more specific rule says otherwise. The other lines open up only what you need. Avoid wildcards like * and avoid unsafe-inline for scripts where you can, since it removes much of the protection. Our page on CSP unsafe-inline explains what to do when your site relies on inline scripts.
Adding it on common platforms
Where you set the header depends on who serves your site. Menu names change between versions, so look for the idea rather than the exact label.
| Platform | Where it usually goes |
|---|---|
| Nginx | add_header line in the server block, then reload Nginx |
| Apache | Header always set in the virtual host or .htaccess (needs mod_headers) |
| Cloudflare | Rules, then Transform Rules, then Modify Response Header |
| WordPress | No core setting: set it in the server, a CDN, or a security headers plugin |
| Next.js | headers() in next.config.js, or middleware if you need nonces |
| Netlify | A _headers file or netlify.toml |
| Vercel | The headers section of vercel.json |
Two typical examples:
# Nginx
add_header Content-Security-Policy "default-src 'self'" always;
# Apache
Header always set Content-Security-Policy "default-src 'self'"
Those are minimal and will break sites that use outside services, so build your real policy in report-only mode first. We have step by step guides for Nginx and WordPress. If your site is behind a CDN or host that sets its own headers, set the CSP there, not on the origin server, or it may be overwritten.
How to confirm the fix worked
- Run
curl -I https://yourdomain.comagain and check that thecontent-security-policyline appears. - Open the site with the browser console open. You should see no blocked-resource errors.
- Test the things that matter to your business: contact form, checkout, login, chat, maps, embedded videos, and any tracking you rely on.
- Run the free checkers again, or wait for your next scan.
If something is broken, the console message names the exact blocked address. Add it to the right part of the policy, or remove the tool if you no longer use it.
How to stop it coming back
- Keep the header in your server or hosting configuration, saved in version control or documented, so a rebuild or migration does not drop it.
- Re-test whenever you add a new third-party tool. A new script from a new domain will be blocked until you allow it.
- Check after changing host, CDN or theme, since these can reset headers.
- Pair it with the related headers: X-Frame-Options is a simple companion, and
frame-ancestorsinside a CSP does the same job more flexibly.
SiteMonday's week 2 security scan checks for this header, and runs only after you prove you own the site. It explains what it found and ranks the fixes, but applying the fix is up to you or your developer.
When to ask for help
If your site uses many outside tools, or a shop with several payment and marketing scripts, building a strict policy takes patience. It is reasonable to hand this to a developer. Even a basic policy is better than none, as long as it is tested before you enforce it.
Step by step for your platform
- Missing Content-Security-Policy header on Nginx
- Missing Content-Security-Policy header on Apache
- Missing Content-Security-Policy header on Cloudflare
- Missing Content-Security-Policy header on WordPress
- Missing Content-Security-Policy header on Next.js
- Missing Content-Security-Policy header on Netlify
- Missing Content-Security-Policy header on Vercel
Questions
Will adding a Content-Security-Policy header break my website?
It can if the policy blocks something your site needs. Start with the `Content-Security-Policy-Report-Only` header, fix what the console reports, then switch to enforcing.
Is a missing CSP header a serious problem?
It is a hardening gap, not a hole by itself. It matters most if an attacker finds a way to inject a script, because a CSP can limit the damage.
Can I add a CSP using a meta tag instead?
Yes, a `<meta http-equiv="Content-Security-Policy">` tag works for many rules, but it cannot do everything, such as `frame-ancestors`. A real HTTP header is the better choice when you can set one.
Does a CSP header help my Google rankings?
Google has not said it is a ranking factor. Add it for security and trust, not for SEO.
What is the easiest policy to start with?
Begin with `default-src 'self'` in report-only mode and add each outside source your site needs. Avoid `*` and avoid `unsafe-inline` for scripts where possible.
Can I set the header on shared hosting without server access?
Often yes, through `.htaccess` on Apache hosts, a host control panel setting, or a CDN such as Cloudflare. If none of those work, ask your host.