Skip to main content
Web Security·2 min read(296 words)·Published 2026-08-25

A Practical Guide to HTTP Security Headers for Web Developers

Why modern web applications need CSP, HSTS, and X-Content-Type-Options, and how to configure them properly in Next.js and Nginx.

5 core sections·Technical reading pace: ~200 WPM

1. Why Defensive Headers Matter

Defensive HTTP response headers instruct the browser on how to handle content safely. They act as an essential second layer of defense. If a stored Cross-Site Scripting (XSS) flaw exists in an application, a strictly configured Content Security Policy can prevent the browser from executing injected JavaScript or exfiltrating session tokens to an unauthorized domain.

2. Content Security Policy (CSP)

CSP is the most powerful and complex header. It restricts where scripts, styles, images, and fonts can be fetched from. A baseline restrictive policy specifies default-src 'self'; and explicitly permits only trusted origins.

Example restrictive CSP header
Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted-cdn.com; object-src 'none'; base-uri 'self'; frame-ancestors 'none';

3. HTTP Strict Transport Security (HSTS)

HSTS (Strict-Transport-Security: max-age=31536000; includeSubDomains; preload) forces the browser to only connect via HTTPS for the specified duration (usually 1 year). It mitigates man-in-the-middle SSL stripping attacks completely.

4. Defending Against Clickjacking & MIME-Sniffing

X-Frame-Options: DENY (or frame-ancestors 'none' in CSP) prevents malicious websites from rendering your application inside a hidden iframe to trick users into clicking buttons. X-Content-Type-Options: nosniff prevents browsers from guessing MIME types if an attacker uploads a script disguised as an image.

5. Clean Next.js Configuration Example

In Next.js App Router, configure headers globally inside next.config.ts using the headers() function. This ensures all static and dynamic responses carry your defensive posture.

next.config.ts header definition
async headers() {
  return [
    {
      source: '/(.*)',
      headers: [
        { key: 'X-Content-Type-Options', value: 'nosniff' },
        { key: 'X-Frame-Options', value: 'SAMEORIGIN' },
        { key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
        { key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' }
      ],
    },
  ];
}

References & Further Reading

Connected Practical Work

GuideWeb Security
How to Check Security Headers of a Website
Learn how to check security headers of a website using browser developer tools and terminal commands to defend against clickjacking, XSS, and MIME sniffing.
Read more
GuideAI Security
10 LLM Security Risks Every Team Should Understand
Examine the top 10 LLM security risks affecting modern applications and learn concrete defensive controls to safeguard your data, models, and infrastructure.
Read more
GuideWeb Security
How to Prevent a DDoS Attack (Step-by-Step Guide for 2026)
Protect your website infrastructure from crashing. Learn the 6 best defensive measures to block malicious traffic, implement rate limiting, and stop DDoS attacks before they cost you revenue.
Read more
GuideWeb Security
How to Protect Your Projects From Malicious npm Packages (and What GitHub's New Package Firewall Does)
GitHub launched a package firewall that blocks risky npm packages before they install. Learn how it works, who can use it and 10 steps any team can take today.
Read more
ReviewSoftware & Services
1Password Review: Enterprise Security and Practical Usability
An independent analysis of 1Password features, dual-key architecture, subscription costs, and platform support for personal and organizational use.
Read more
News2026-11-09
Cybersecurity News Briefing: November 9, 2026
Today's briefing highlights two key themes: rapid credential abuse in recent infostealer attacks and the push for fully domestic hardware in China. Elastic’s analysis of 27 incidents shows attackers can log in with stolen credentials within an average eight minutes, while Huawei’s new Qingyun HM650 desktop claims entirely Chinese components, though verification is pending. Readers should prioritize reviewing and tightening credential monitoring policies.
Read more