0
0
Nginxdevops~10 mins

Proxy headers in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Proxy headers
Client sends request
Nginx receives request
Nginx adds/updates proxy headers
Nginx forwards request to backend server
Backend server receives request with headers
Backend processes request and responds
Nginx sends response back to client
The flow shows how Nginx receives a client request, adds or modifies proxy headers, then forwards it to the backend server, which uses these headers for processing.
Execution Sample
Nginx
location /api/ {
  proxy_pass http://backend;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
This Nginx config forwards requests to /api/ to a backend server, adding headers that tell the backend the original host and client IP.
Process Table
StepActionHeader SetValueEffect
1Receive client requestNoneNoneNginx gets request from client
2Set Host headerHost$host (example.com)Backend knows original host
3Set X-Real-IP headerX-Real-IP$remote_addr (192.0.2.1)Backend knows client IP
4Set X-Forwarded-For headerX-Forwarded-For$proxy_add_x_forwarded_for (192.0.2.1)Tracks client IP chain
5Forward requestAll above headersSent to backendBackend receives headers
6Backend processes requestReads headersUses for logging/securityResponds accordingly
7Nginx sends responseNoneNoneClient receives backend response
💡 Request fully proxied with headers set, backend processed and responded
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Hostunsetexample.comexample.comexample.comexample.com
X-Real-IPunsetunset192.0.2.1192.0.2.1192.0.2.1
X-Forwarded-Forunsetunsetunset192.0.2.1192.0.2.1
Key Moments - 3 Insights
Why do we set the Host header explicitly in proxy headers?
Because the backend server needs to know the original host requested by the client. Without setting Host, the backend might see the proxy's hostname instead, which can cause wrong responses. See execution_table step 2.
What is the difference between X-Real-IP and X-Forwarded-For headers?
X-Real-IP carries the immediate client's IP address, while X-Forwarded-For can hold a chain of IPs if multiple proxies are involved. This helps backend track the original client IP. See execution_table steps 3 and 4.
What happens if proxy headers are not set correctly?
The backend may not know the real client IP or host, which can break logging, security checks, or routing. The request might still work but with incomplete info. See execution_table steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does the X-Real-IP header have after step 3?
A192.0.2.1
Bexample.com
Cunset
Dproxy server IP
💡 Hint
Check the 'Header Set' and 'Value' columns at step 3 in execution_table.
At which step does Nginx forward the request to the backend with all proxy headers set?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the step where the action is 'Forward request' in execution_table.
If the X-Forwarded-For header was not set, what would change in the variable_tracker?
AX-Real-IP would be unset
BX-Forwarded-For would remain unset after all steps
CHost would be unset
DNo variables would change
💡 Hint
Check the variable_tracker row for X-Forwarded-For and imagine skipping step 4.
Concept Snapshot
Proxy headers in Nginx add info about the original client request when forwarding.
Use proxy_set_header to set Host, X-Real-IP, and X-Forwarded-For.
Host tells backend the original domain.
X-Real-IP gives backend the client IP.
X-Forwarded-For tracks client IP chain through proxies.
Proper headers help backend logging and security.
Full Transcript
This visual execution shows how Nginx handles proxy headers. When a client sends a request, Nginx receives it and sets headers like Host, X-Real-IP, and X-Forwarded-For before forwarding to the backend server. The Host header tells the backend the original domain requested. X-Real-IP carries the client's IP address. X-Forwarded-For tracks the chain of client IPs if multiple proxies are involved. The backend uses these headers for logging and security. If headers are missing or incorrect, the backend may not get accurate client info. The execution table traces each step, showing header values and effects. The variable tracker shows how header values change step-by-step. Key moments clarify why each header matters. The quiz tests understanding of header values and flow. This helps beginners see how proxy headers work in Nginx.