π Essential Nginx Headers for Requests & Responses
Headers help pass additional information in requests and responses. They can enhance security, manage caching, and provide context to backend servers. Below is a list of commonly used headers in Nginx with their purposes and examples.
πΉ 1. Host Headerβ
Purpose: Tells the backend server the domain the user is requesting.
proxy_set_header Host $host;
π Explanation:
- If a user visits
example.com
, Nginx forwards this as theHost
header. - This allows the backend to know which domain is being accessed.
πΉ 2. X-Real-IP Headerβ
Purpose: Passes the real IP address of the user to the backend server.
proxy_set_header X-Real-IP $remote_addr;
π Explanation:
- If the userβs IP address is
192.168.1.1
, this header ensures the backend sees the actual client IP instead of Nginxβs IP.
πΉ 3. X-Forwarded-For Headerβ
Purpose: Tracks all IP addresses that handled the request before reaching the backend.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
π Explanation:
- Useful for logging and tracking the real origin of requests.
- Helps when multiple proxies are involved.
πΉ 4. X-Forwarded-Proto Headerβ
Purpose: Indicates whether the original request was made over HTTP or HTTPS.
proxy_set_header X-Forwarded-Proto $scheme;
π Explanation:
- If the user connects via
HTTPS
, this header tells the backend that the request is secure.
πΉ 5. Cache-Control Headerβ
Purpose: Manages browser caching behavior for files served by Nginx.
add_header Cache-Control "public, max-age=3600";
π Explanation:
- Tells browsers to cache files for 1 hour (
3600
seconds) to speed up repeated visits.
πΉ 6. Content-Type Headerβ
Purpose: Specifies the type of content being served (e.g., HTML, JSON, images).
add_header Content-Type "text/html";
π Explanation:
- Ensures the browser correctly interprets the content type.
πΉ 7. X-Content-Type-Options Headerβ
Purpose: Prevents the browser from interpreting files as a different type than declared.
add_header X-Content-Type-Options "nosniff";
π Explanation:
- Improves security by ensuring that a
.jpg
file is always treated as an image.
πΉ 8. X-Frame-Options Headerβ
Purpose: Prevents clickjacking attacks by restricting iframe embedding.
add_header X-Frame-Options "SAMEORIGIN";
π Explanation:
- Only allows embedding on the same domain.
- Prevents malicious sites from loading your site in an iframe.
πΉ 9. Strict-Transport-Security (HSTS) Headerβ
Purpose: Forces browsers to always use HTTPS.
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
π Explanation:
- Enforces HTTPS for a year (
31536000
seconds) across the domain and its subdomains.
πΉ 10. Access-Control-Allow-Origin Header (CORS)β
Purpose: Enables Cross-Origin Resource Sharing (CORS) to allow resources to be accessed from other domains.
add_header Access-Control-Allow-Origin "*";
π Explanation:
- Lets any domain (
*
) access your resources. - Useful for APIs or fonts.
πΉ 11. Referrer-Policy Headerβ
Purpose: Controls how much referrer information is shared when users navigate from your site.
add_header Referrer-Policy "no-referrer";
π Explanation:
- No referrer information is sent when users click a link.
- Improves privacy.
πΉ 12. Custom Headersβ
You can add your own headers to pass custom information.
add_header X-Custom-Header "Hello, World!";
π Explanation:
- This adds a custom header that says "Hello, World!".
- It can be read by the backend or front-end scripts.
π Real-Life Analogyβ
Headers are like notes passed along with a message:
β‘οΈ User β Nginx β Backend Server: Headers tell the backend extra details about the user's request (like who they are, where they came from, or how to handle their request).
π Example:
- "Tell the backend the user is connecting securely (HTTPS) and their real IP is
192.168.1.1
."
πΉ Host Header β Like writing the recipientβs name on an envelope, so it reaches the right person.
πΉ X-Real-IP Header β Like including a return address, so the receiver knows who sent the letter.
πΉ X-Forwarded-Proto Header β Like marking a letter as βURGENTβ to indicate priority (HTTP vs HTTPS).
πΉ Cache-Control Header β Like instructing a librarian to keep a book reserved for a week.
πΉ X-Frame-Options Header β Like setting rules that only certain people can view a private document.
π― Using these headers effectively can improve security, performance, and functionality in your Nginx setup!