Back to cheatsheets
Web Security
Security Headers Configuration
Essential HTTP security headers with copy-paste Nginx and Apache configs for CSP, HSTS, and more.
What you'll learn
- Nginx config
- Apache .htaccess
- Express.js middleware
How to use these snippets
- Click the Copy button next to any snippet to copy it to your clipboard.
- Paste the code into your project, IDE, or terminal.
- Replace placeholder values (like
example.comoryour-secret) with your actual data. - Review the code comments for important security notes and caveats.
- Try the interactive related tool to see the concept in action.
Code Snippets
Copy-paste code snippets
3 ready-to-use snippets for security headers configuration. Click copy and paste into your project.
Nginx config
add_header Content-Security-Policy
"default-src 'self'; script-src 'self'";
add_header Strict-Transport-Security
"max-age=63072000; includeSubDomains; preload";
add_header X-Content-Type-Options "nosniff";
add_header X-Frame-Options "DENY";
add_header Referrer-Policy
"strict-origin-when-cross-origin";
add_header Permissions-Policy
"camera=(), microphone=(), geolocation=()";
Apache .htaccess
<IfModule mod_headers.c>
Header set Content-Security-Policy
"default-src 'self'"
Header set Strict-Transport-Security
"max-age=63072000; includeSubDomains; preload"
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "DENY"
Header set Referrer-Policy
"strict-origin-when-cross-origin"
Header set Permissions-Policy
"camera=(), microphone=(), geolocation=()"
</IfModule>
Express.js middleware
const helmet = require('helmet');
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", 'data:', 'https:'],
}
},
hsts: { maxAge: 63072000, includeSubDomains: true, preload: true }
}));
Tool
Related ShieldX tool
Try this concept hands-on with the interactive tool.
100% Free · No signup · No cost
Generate something unguessable right now
148 bits of entropy, a crack-time estimate you can trust, and a clipboard that clears itself after 30 seconds.
Part of the Toolly ecosystem · secure.toolly.site