Content Security Policy (CSP): How to Check, Fix and Configure It on Your Website
Learn how to check, add and fix Content Security Policy headers on Apache, Nginx, WordPress and Joomla, with practical diagnostic tools and hosting guidance.
HTTPS and software updates are essential, but they do not prevent every malicious script from running in a visitor's browser. Content Security Policy (CSP) adds browser-enforced restrictions on the resources a page may load and execute. It helps mitigate certain cross-site scripting (XSS) attacks, but does not replace contextual output escaping, input validation or dependency updates.
This practical tutorial shows how to inspect your current policy, find its source, introduce changes safely, and fix problems on Apache, Nginx, WordPress and Joomla.
1. Check your existing CSP
Chrome or Firefox: Open the page, press F12, choose Network, reload, click the HTML document, and inspect Response Headers. Look for Content-Security-Policy (enforced) or Content-Security-Policy-Report-Only (diagnostic only). Check the Console for violations.
Command line: Run curl -I https://example.com/. On macOS/Linux, filter the response with curl -sSI https://example.com/ | grep -i content-security-policy. HEAD responses can differ from GET responses, so use browser DevTools to confirm.
Online tools: SecurityHeaders.com checks security headers; Google CSP Evaluator flags weaknesses in a policy. Neither proves the entire website is secure.
No CSP header on one response does not establish that every page lacks CSP. Also inspect any <meta http-equiv="Content-Security-Policy"> tag: meta policies have limitations, including no support for frame-ancestors or Report-Only.
2. Find where the policy is defined
| Environment | Likely location |
|---|---|
| Apache / compatible LiteSpeed | .htaccess, virtual host or server configuration |
| Nginx | server / location configuration |
| PHP | Application code using header() |
| WordPress | Plugin, custom code, reverse proxy or host |
| Joomla | System – HTTP Headers plugin, server or proxy |
| Cloudflare | Response Header Transform Rules |
| Managed hosting | Hosting panel or provider-managed configuration |
Inspect existing response headers before adding another. Browsers enforce multiple CSP policies cumulatively; a new permissive header does not cancel an existing restrictive one. Check CDN and hosting rules too.
3. Add CSP in Apache using .htaccess
Using your host's file manager or SFTP (FileZilla/WinSCP), locate the public directory containing index.php, often public_html or httpdocs. Enable hidden files to see .htaccess. Download a backup, and avoid placing custom rules inside a CMS-managed rewrite block.
If Apache's mod_headers is enabled and the host permits these directives, add:
<IfModule mod_headers.c>
Header always set Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'"
</IfModule>
Reload and verify the header under Network → Headers. Check the Console for violations. Report-Only reports possible restrictions but does not block the resources. This example is diagnostic, not a complete deployment policy.
If a trusted CDN script is reported, first confirm its purpose and ownership. You might adapt the directive to script-src 'self' https://cdn.example.com; do not add every blocked host blindly. Host allowlists are less robust against XSS than well-designed nonce/hash policies.
After testing login, forms, checkout, editors, and other workflows, you may replace Content-Security-Policy-Report-Only with Content-Security-Policy to enforce your tested policy. If the site breaks, restore the backed-up file, clear server/CDN caches and retest. An HTTP 500 after editing suggests a syntax or server-permission problem.

4. Configure CSP on Nginx
Nginx ignores .htaccess. If you administer the server, inspect its active configuration (often /etc/nginx/sites-available/ or /etc/nginx/conf.d/). In the relevant server block, use:
add_header Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'" always;
Test with sudo nginx -t, then reload with sudo systemctl reload nginx. Watch for add_header inheritance and rules in more-specific locations. On managed hosting without Nginx configuration access, use the provider's control panel or support.
5. WordPress: plugin or small custom integration
No-code route: First check whether your host or an existing security-header plugin already sends CSP. Choose a maintained plugin that explicitly supports CSP and preferably Report-Only; do not assume a general security plugin supports either. In its settings, enable Report-Only, start with a conservative diagnostic policy, save, and test both public pages and wp-admin before enforcement.
Developer route: Create wp-content/plugins/ceikay-csp/ceikay-csp.php:
<?php
/**
* Plugin Name: CeiKay CSP Example
* Description: CSP diagnostics on the WordPress frontend.
*/
add_action('send_headers', function () {
if (is_admin()) {
return;
}
header(
"Content-Security-Policy-Report-Only: "
. "default-src 'self'; "
. "script-src 'self'; "
. "object-src 'none'; "
. "base-uri 'self'"
);
});
Activate it in Plugins → Installed Plugins, then confirm the response header. This does not override an existing CSP from the web server, CDN or another plugin. It deliberately excludes wp-admin and does not provide a complete enforced policy.
Troubleshooting: If a CAPTCHA fails, inspect blocked script-src, frame-src and connect-src requests and consult the CAPTCHA provider's integration guidance. If a page builder reports blocked inline scripts, check for support for CSP nonces or hashes. Do not automatically add 'unsafe-inline'. For per-response nonces, ensure full-page caching does not reuse the same nonce and HTML across visitors.
6. Joomla: use the native HTTP Headers plugin
In Joomla administration, open System → Plugins, search for HTTP Headers, and open System – HTTP Headers. Exact menu wording varies by Joomla version and language.
Enable its CSP options and begin with Report-Only on the public site. Start by diagnosing a directive such as script-src 'self'; save and inspect the Console. Test templates, page builders, editors, contact forms, CAPTCHAs, payment integrations and media features.
If you see Refused to execute inline script, identify which template or extension generates it. Prefer a compatible nonce or hash approach where supported; validate that the script is legitimate before authorizing it. Test the administrator separately from the frontend. If the server already sets CSP, reconcile the configurations rather than stacking conflicting policies.
Consult the Joomla HTTP Headers documentation for the options supported by your installed version.
7. Fix the actual violation, not the entire policy
| Violation | What to investigate | Typical fix |
|---|---|---|
script-src |
External or inline JavaScript | Verify source; use appropriate nonce/hash or narrowly scoped source |
style-src |
Stylesheet or inline CSS | Inspect generated styles; use approved source or compatible hash/nonce |
img-src |
External image or data URL | Permit a verified image host only if required |
connect-src |
fetch/XHR/WebSocket | Verify API origin and add narrowly scoped permission |
frame-src |
Embedded video/CAPTCHA/payment frame | Follow provider's documented domains |
form-action |
Form submission target | Permit legitimate submission origin |
frame-ancestors |
Other site embedding your page | Explicitly allow intended parent origins, or use 'none' |
For example, if a legitimate image at https://images.example.com/photo.webp is blocked by img-src 'self', change the relevant directive to img-src 'self' https://images.example.com after confirming that host is trusted. Do not weaken default-src merely to fix one image.

Some directives, including frame-ancestors and form-action, do not fall back to default-src. Include them explicitly when needed.
8. Does hosting affect CSP configuration?
Yes—the CSP browser standard stays the same, but your control over response headers changes. Apache shared hosting may expose .htaccess; managed Nginx may require a provider panel; VPS administrators can modify server configuration; and CDNs may add headers after the application generates the response.
Before choosing a hosting service, ask: Can I set and remove CSP headers without root access? Can I use Report-Only? Can I inspect the final response? Are custom headers available on my plan? Can support explain conflicts with CDN or application rules?
Cloudways: hosting-level HTTP header controls
Cloudways documents Web Rules → Header Rules for managing HTTP response headers, including CSP. This offers a hosting-level alternative to editing application files, subject to the features available for your application and plan. It does not automatically create a safe CSP; you must still test and maintain your directives.
9. Stronger protection with nonce-based CSP
A strict CSP uses unpredictable nonces or hashes rather than relying only on domain allowlists. A simplified PHP example:
<?php
$nonce = base64_encode(random_bytes(16));
header(
"Content-Security-Policy: "
. "script-src 'nonce-{$nonce}' 'strict-dynamic'; "
. "object-src 'none'; "
. "base-uri 'none'"
);
In the HTML template:
<script nonce="<?= htmlspecialchars($nonce, ENT_QUOTES, 'UTF-8') ?>">
console.log('Trusted script');
</script>
Generate a fresh unpredictable nonce for each response. Keep the header and HTML nonce consistent, and plan around reverse proxies and full-page caches. With 'strict-dynamic', a trusted script may load further scripts, so audit trusted loaders carefully. This is an illustration, not a drop-in complete policy for all applications.
10. Final deployment checklist
- Identify all existing CSP headers and meta policies.
- Back up server and CMS configuration.
- Inventory scripts, styles, frames, APIs, images and external services.
- Deploy a Report-Only policy first.
- Investigate every violation and authorize only trusted, necessary behavior.
- Test frontend, administration, login, checkout, forms and integrations.
- Check caching, nonces, CDN behavior and duplicate headers.
- Enable enforcement gradually and monitor after software updates.
Conclusion
A useful CSP is not simply a header copied from a checklist. Inspect your current configuration, find where it is defined, test in Report-Only mode, fix the precise compatibility issues, and only then enforce a policy appropriate to your website. WordPress and Joomla administrators can start with CMS tooling; developers can use Apache, Nginx or PHP; and hosting platforms may offer direct header controls.
CSP reduces attack opportunities, but remains one layer alongside secure coding, updates, authentication and monitoring.