How to add security headers to your website
Updated 7/20/2026
Security headers are instructions the server sends to the browser to block common attacks. They are added in a few lines.
Nginx
In the server block, add:
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 Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
nginx -t && systemctl reload nginx
What these headers do
- X-Frame-Options: prevents the site from loading in an iframe (anti-clickjacking protection).
- X-Content-Type-Options: stops the browser from guessing file types.
- Referrer-Policy: controls what referrer information is sent to other sites.
- Strict-Transport-Security: forces HTTPS for future visits.
Content Security Policy
For advanced protection against injected scripts, add a CSP header, but test it carefully so you do not block legitimate resources:
add_header Content-Security-Policy "default-src 'self'" always;
Check the result with free online header-analysis tools. See also how to add HTTPS.
Put this guide into practice on your own VPS
EU cloud servers, billed hourly, ready in minutes, with one-click installs for dozens of apps.
See pricingRelated tutorials
How to scan your server for malware with ClamAV
Install ClamAV, an open-source antivirus, and periodically scan your server for infected files.
Updated 7/20/2026SecurityHow to check which ports are open on your server
Find out which services are listening for connections on your server and close what you do not need, for less exposure.
Updated 7/20/2026SecurityHow to limit abusive traffic with Nginx (rate limiting)
Protect your site from bots and simple attacks by limiting the number of requests per second from an IP.
Updated 7/20/2026