What if your server could see exactly who's visiting, even behind layers of proxies?
Why Proxy headers forwarding in Nginx? - Purpose & Use Cases
Imagine you run a website behind a proxy server, and you want to know the real visitor's IP address and other details.
Without forwarding headers, your backend only sees the proxy's info, not the actual user.
Manually trying to track real user info is like guessing who called you when everyone calls from the same phone number.
This makes logging, security checks, and personalization unreliable and frustrating.
Proxy headers forwarding lets the proxy send real user info to your backend automatically.
This way, your server knows exactly who is visiting, improving logs, security, and user experience.
proxy_pass http://backend;
# No headers forwarded, backend sees proxy IP onlyproxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://backend;
It enables accurate user tracking and better security by passing real client details through proxies.
A company uses a proxy to protect their website but still needs to block suspicious IPs; forwarding headers lets their backend see real visitor IPs to block bad actors effectively.
Manual setups hide real user info behind proxies.
Forwarding headers shares real client details with backend servers.
This improves logging, security, and user personalization.
Practice
proxy_set_header in an nginx proxy configuration?Solution
Step 1: Understand proxy_set_header role
The directiveproxy_set_headeris used to set or override HTTP headers sent to the backend server.Step 2: Identify what headers are forwarded
It forwards client info such as IP address, Host, and protocol to help backend servers process requests correctly.Final Answer:
To forward original client information like IP and Host to the backend server -> Option DQuick Check:
proxy_set_header forwards client info = D [OK]
- Thinking it blocks requests
- Confusing it with caching or compression
- Assuming it modifies client requests directly
Solution
Step 1: Identify correct directive for setting headers
The correct directive to set headers in nginx proxy isproxy_set_header.Step 2: Use correct variable for client IP
The variable$remote_addrholds the original client IP address.Final Answer:
proxy_set_header X-Real-IP $remote_addr; -> Option CQuick Check:
proxy_set_header + $remote_addr = B [OK]
- Using wrong directive like proxy_pass_header
- Using $proxy_addr which is invalid
- Confusing proxy_set_header with proxy_forward_header
location /api/ {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}What will be the value of the
X-Forwarded-For header sent to the backend if the client IP is 192.168.1.10 and the header already contains 10.0.0.1?Solution
Step 1: Understand $proxy_add_x_forwarded_for variable
This variable appends the client IP to the existing X-Forwarded-For header, separated by a comma.Step 2: Combine existing and client IP
Since the header already has 10.0.0.1, and client IP is 192.168.1.10, the result is "10.0.0.1, 192.168.1.10".Final Answer:
10.0.0.1, 192.168.1.10 -> Option BQuick Check:
$proxy_add_x_forwarded_for appends IP = C [OK]
- Assuming it replaces the header instead of appending
- Confusing $proxy_add_x_forwarded_for with $remote_addr
- Expecting only client IP without existing values
proxy_set_header X-Forwarded-Proto $scheme;
But the backend always receives
http even when client uses https. What is the likely cause?Solution
Step 1: Understand $scheme variable behavior
$scheme reflects the protocol used by the client to connect to nginx (http or https).Step 2: Check nginx HTTPS configuration
If nginx is not listening on HTTPS, all requests come as http, so $scheme is always "http".Final Answer:
nginx is not configured to listen on HTTPS, so $scheme is always http -> Option AQuick Check:
$scheme depends on nginx listener protocol = A [OK]
- Thinking proxy_set_header syntax is wrong
- Assuming backend ignores header without checking
- Using $https variable which is not standard
Solution
Step 1: Verify variables for client IP, host, and protocol
$remote_addr is client IP, $host is original host header, and $scheme is protocol (http or https).Step 2: Check each option for correctness
proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; uses correct variables and syntax. Others use invalid variables like $proxy_addr, $proxy_host, or $http_scheme which do not exist.Final Answer:
proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; -> Option AQuick Check:
Use $remote_addr, $host, $scheme for forwarding headers = A [OK]
- Using non-existent variables like $proxy_addr or $proxy_host
- Confusing $https with $scheme
- Missing one or more headers to forward
Ready to prove it?
Pass the test and this pattern moves unknown
