Security headers and CSP generator
Pick what your site needs and copy ready config for nginx, Apache or a Laravel middleware: HSTS, Content-Security-Policy, X-Frame-Options, X-Content-Type-Options, Referrer-Policy and Permissions-Policy. Start from your live site and the CSP fills itself with the domains your page uses.
# In the server { } block of the site; "always" adds them to error pages too
add_header Strict-Transport-Security "max-age=31536000" always;
add_header Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=(), browsing-topics=()" always;
# .htaccess or the VirtualHost; needs mod_headers
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000"
Header always set Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=(), browsing-topics=()"
</IfModule>
<?php
// app/Http/Middleware/SecurityHeaders.php
// Register in bootstrap/app.php: ->withMiddleware(fn (Middleware $m) => $m->append(SecurityHeaders::class))
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class SecurityHeaders
{
private const HEADERS = [
'Strict-Transport-Security' => "max-age=31536000",
'Content-Security-Policy-Report-Only' => "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests",
'X-Frame-Options' => "SAMEORIGIN",
'X-Content-Type-Options' => "nosniff",
'Referrer-Policy' => "strict-origin-when-cross-origin",
'Permissions-Policy' => "camera=(), microphone=(), geolocation=(), payment=(), usb=(), browsing-topics=()",
];
public function handle(Request $request, Closure $next): Response
{
$response = $next($request);
foreach (self::HEADERS as $name => $value) {
$response->headers->set($name, $value);
}
return $response;
}
}
Strict-Transport-Security: max-age=31536000
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=(), usb=(), browsing-topics=()
The policy is in test mode: open the site with the browser console (F12) and add the domains it reports, then switch to Enforce.
After adding the headers, check example.com with the SSL and security check.
How it works
Three steps, a minute.
- 01
Start from your site
Optional: we read the headers your site sends and the outside domains its page loads, and fill the form.
- 02
Choose the options
Turn headers on and off, pick services for the CSP and add your own domains. The config changes as you click.
- 03
Copy the config
Copy the lines for nginx, Apache or a ready Laravel middleware, add them and test the CSP in Report-Only mode first.
What it builds
Three groups of headers.
HTTPS only
Strict-Transport-Security with a lifetime from 5 minutes for testing to 2 years for the preload list, with subdomains if you want.
Basic protection
X-Frame-Options against clickjacking, nosniff, Referrer-Policy, Permissions-Policy for 15 browser features and Cross-Origin-Opener-Policy.
Content-Security-Policy
A policy with presets for 13 services (Google Analytics, Ads, Fonts, Maps, reCAPTCHA, YouTube, Meta Pixel, Clarity, Hotjar and others), your domains and a report address.
FAQ
Will a Content-Security-Policy break my site?
It can, if it misses a domain the site uses. That is why the generator starts in Report-Only mode: nothing is blocked, and the browser console lists what would be. Add those domains, and switch to Enforce when the console is clean.
Why is 'unsafe-inline' on by default?
Most sites have inline scripts and style="…" attributes, and a policy without 'unsafe-inline' blocks them. A CSP with it still stops scripts from unknown domains. Removing it needs nonces or hashes in the site's code; turn it off only after that.
Should I turn on HSTS preload?
Only when the domain and every subdomain work over HTTPS and will stay so. Preload puts the domain into the list built into browsers; getting out takes months.
Where do I check the result?
After adding the headers, run the SSL and security check on your site: it scores the same headers and the certificate.
Is it limited?
Building headers is free and unlimited. Reading a live site is limited to 20 times per hour from one address.
Want the headers set up and tested for you?
I will add the security headers to your server, build a Content-Security-Policy that does not break the site, and move it from test mode to enforcement.