Key: value) that tell clients, servers, and intermediaries how to handle the payload and connection. Think of headers like the address, postage, and delivery instructions on a physical envelope — they don’t contain the letter (body) but they tell everyone how to process it.
This guide explains the most common HTTP header categories, shows realistic examples, and demonstrates how to configure headers in NGINX. It maintains the original examples while improving readability and structure for quick reference.

Quick overview: header categories
Request example (HTTP/1.1)
A typical browser request contains many request headers that describe the client, acceptable media types, language, caching preferences, and more.Hostidentifies the target host (required for virtual hosting).User-Agentidentifies the client software and platform.AcceptandAccept-Languageguide content negotiation.
HTTP/2 pseudo-headers + modern request example
Some transports (HTTP/2) include pseudo-headers (beginning with:) together with standard headers:
Sec-CH-*headers are part of Client Hints for improved server-side decisions.Cookietransmits session or user state to the server.
Response example (common response headers)
Servers include response headers that describe the payload, caching, compression, and server metadata:Content-Type— MIME type and charset.Content-Encoding— compression used (gzip, br).Vary— instructs caches that different request headers produce different responses.
Security headers — purpose and examples
Security headers defend against common web threats such as XSS, clickjacking, and protocol downgrade. Example of a secure application’s headers (trimmed for clarity):Content-Security-Policy(CSP) — restricts sources for scripts, styles, frames, etc., and can use nonces or hashes for fine-grained control.X-Content-Type-Options: nosniff— prevents MIME sniffing, forcing the browser to obeyContent-Type.X-Frame-Options: SAMEORIGIN— blocks framing by other origins (mitigates clickjacking).Strict-Transport-Security(HSTS) — enforces HTTPS for future requests.
Security headers like
Cache-Control, Pragma, and Expires are often combined with CSP and HSTS to prevent sensitive pages from being cached and to harden the application against client-side attacks.Authentication headers
Authentication credentials are commonly passed in headers. Examples:Authorization: Basic <base64>— legacy Basic Auth, where the base64 decodes tousername:password. Only use over TLS and avoid in modern systems.Authorization: Bearer <token>— common for token-based authentication (OAuth2, JWT). Always protect tokens and avoid logging them.
Do not log full
Authorization header values or other sensitive headers (tokens, passwords) in production logs. Mask or truncate these fields to prevent leakage.Caching headers and how caching works
Caching headers tell browsers and intermediaries how long to store responses and when to revalidate. Key caching headers:Cache-Control— modern and flexible; supportsmax-age,no-cache,no-store,must-revalidate.Expires— older, date-based expiry.Etag— entity tag for versioning; used withIf-None-Matchto validate resources.Vary— lists request headers that affect the cache (e.g.,Accept-Encoding).
Etagenables conditional GETs: client sendsIf-None-Matchand server may return304 Not Modified.- CDNs and reverse proxies (Varnish, CloudFront) often add
X-CacheorViaheaders for debugging cache behavior.
CORS (Cross-Origin Resource Sharing)
CORS is a browser security feature that restricts cross-origin HTTP requests. Servers explicitly allow cross-origin access using CORS response headers.
Access-Control-Allow-Origin— allowed origin(s) or*.Access-Control-Allow-Methods— allowed HTTP methods for the cross-origin request.Access-Control-Allow-Headers— allowed request headers the client may send.Access-Control-Allow-Credentials— whether cookies/credentials are allowed.
Proxy headers
When requests traverse proxies or load balancers, they commonly add headers to preserve original client information. Handle these carefully and only trust them from known, internal proxies. Common proxy headers:X-Forwarded-For— original client IP and proxy chain.X-Forwarded-Host— originalHostheader.X-Forwarded-Proto— original scheme (httporhttps).X-Forwarded-Server— proxy hostname.X-Real-IP— client IP (used when a single proxy exists).
- Only trust these headers when they are set by your own proxies/load balancers.
- Use strict access controls and validation to avoid spoofed client IPs.
Custom headers
You can define custom headers for application-specific metadata. Historically, they used anX- prefix but modern practice prefers descriptive, namespaced headers without the X- prefix.
Example:
NGINX built-in variables (common ones)
NGINX exposes many dynamic variables that you can use in configuration files. These are prefixed with$.
To construct a full URL in NGINX:
https://www.example.com/path?query.
Example NGINX configuration: set response headers + forward request headers
This concise NGINX example demonstrates how to add security and cache headers to responses and how to forward original request details to an upstream backend.add_headersets headers on NGINX responses. Usealwaysto ensure headers are added even on error responses.proxy_set_headercontrols headers sent to upstream servers — useful for preserving client IP, host, and scheme.
Summary
- HTTP headers are essential metadata used by clients, servers, and intermediaries to coordinate content negotiation, caching, authentication, security, and routing.
- Security and CORS headers are critical for protecting web applications; misconfiguration can lead to vulnerabilities or broken integrations.
- When using proxies and CDNs, pay attention to proxy headers and cache-related headers.
- In NGINX, use
add_headerto set response headers andproxy_set_headerto preserve client/request info for upstreams.
Links and references
- MDN Web Docs — HTTP headers
- RFC 7231 — HTTP/1.1 Semantics and Content
- OWASP Secure Headers Project
- NGINX Documentation — Variables
- MDN — CORS
curl -I / curl -v), or network proxies, and practice configuring NGINX to add, modify, and strip headers for different environments.