0
0
Nginxdevops~20 mins

Proxy headers forwarding in Nginx - Mini Project: Build & Apply

Choose your learning style9 modes available
Proxy Headers Forwarding with Nginx
📖 Scenario: You are setting up a web server using Nginx as a reverse proxy. Your goal is to forward specific HTTP headers from the client to the backend server to preserve client information.
🎯 Goal: Configure Nginx to forward the X-Real-IP and X-Forwarded-For headers to the backend server.
📋 What You'll Learn
Create a basic Nginx server block listening on port 80
Set the backend server URL as http://backend.local
Add proxy headers X-Real-IP and X-Forwarded-For forwarding
Print the final Nginx configuration to verify the headers forwarding
💡 Why This Matters
🌍 Real World
In real websites, Nginx often acts as a reverse proxy. Forwarding headers like <code>X-Real-IP</code> helps backend servers log and respond based on the actual client IP, not the proxy's IP.
💼 Career
Understanding proxy headers is essential for DevOps roles managing web servers and load balancers to ensure accurate client information is preserved and security policies are correctly applied.
Progress0 / 4 steps
1
Create basic Nginx server block
Create an Nginx server block listening on port 80 with a location / that proxies requests to http://backend.local. Name the server block server.
Nginx
Need a hint?

Use listen 80; inside server { } and proxy_pass http://backend.local; inside location / { }.

2
Add proxy header for X-Real-IP
Inside the location / block, add the line proxy_set_header X-Real-IP $remote_addr; to forward the client's real IP address.
Nginx
Need a hint?

Use proxy_set_header X-Real-IP $remote_addr; inside the location / block.

3
Add proxy header for X-Forwarded-For
Inside the location / block, add the line proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; to forward the chain of client IP addresses.
Nginx
Need a hint?

Use proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; inside the location / block.

4
Print the final Nginx configuration
Print the complete Nginx server block configuration to verify the proxy headers forwarding.
Nginx
Need a hint?

Use a print statement to display the full configuration as shown.