Skip to main content
HTTP headers are the metadata “envelope” that travels with every request and response on the web. They are simple key-value pairs (format: 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.
A presentation slide titled "Types of HTTP Headers" showing two categories: a "General Headers" box and a "Request/Response Headers" box. The request/response box lists header types such as Security, Authentication, Caching, CORS, Proxy, and Custom Headers.

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.
Key points:
  • Host identifies the target host (required for virtual hosting).
  • User-Agent identifies the client software and platform.
  • Accept and Accept-Language guide 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.
  • Cookie transmits 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:
Notable headers:
  • 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):
Important security headers:
  • 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 obey Content-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 to username: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; supports max-age, no-cache, no-store, must-revalidate.
  • Expires — older, date-based expiry.
  • Etag — entity tag for versioning; used with If-None-Match to validate resources.
  • Vary — lists request headers that affect the cache (e.g., Accept-Encoding).
Example response with cache-related fields:
  • Etag enables conditional GETs: client sends If-None-Match and server may return 304 Not Modified.
  • CDNs and reverse proxies (Varnish, CloudFront) often add X-Cache or Via headers for debugging cache behavior.
When deploying behind a proxy/CDN, configure caching at the edge to reduce origin traffic and improve latency.

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.
An infographic titled "CORS Header" showing a web browser, a globe/network and servers connected by dashed arrows labeled "Retrieves data" and "Includes CORS headers." It notes that CORS controls resource requests from different domains and restricts external website access to protect data.
Example of an OPTIONS preflight response:
Key CORS 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.
CORS must be coordinated between client and server domains; incorrect CORS settings can break legitimate cross-domain interactions or, conversely, open up security exposure.

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 — original Host header.
  • X-Forwarded-Proto — original scheme (http or https).
  • X-Forwarded-Server — proxy hostname.
  • X-Real-IP — client IP (used when a single proxy exists).
Example (environment-style display):
Best practices:
  • 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 an X- prefix but modern practice prefers descriptive, namespaced headers without the X- prefix. Example:
Use cases: feature toggles, build/version identifiers, routing hints. Never put sensitive data in custom headers unless it is encrypted and protected.

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:
This results in 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.
Notes:
  • add_header sets headers on NGINX responses. Use always to ensure headers are added even on error responses.
  • proxy_set_header controls 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_header to set response headers and proxy_set_header to preserve client/request info for upstreams.
In follow-up lessons you can inspect headers with browser devtools, curl (curl -I / curl -v), or network proxies, and practice configuring NGINX to add, modify, and strip headers for different environments.

Watch Video