What is the purpose of forwarding headers in an Nginx proxy setup?
Forwarding headers allows the proxy server to pass important client information, like the original IP address and protocol, to the backend server. This helps the backend understand the real client details.
Click to reveal answer
beginner
Which Nginx directive is commonly used to forward the original client IP address to the backend?
The directive proxy_set_header X-Real-IP $remote_addr; forwards the client's IP address to the backend server.
Click to reveal answer
intermediate
What does the X-Forwarded-For header represent in proxy forwarding?
It lists the IP addresses of the client and all proxies the request passed through, helping the backend identify the original client IP even behind multiple proxies.
Click to reveal answer
beginner
How do you forward the original protocol (HTTP or HTTPS) in Nginx proxy headers?
Use proxy_set_header X-Forwarded-Proto $scheme; to forward the protocol used by the client to the backend server.
Click to reveal answer
intermediate
Why is it important to set Host header when proxying requests in Nginx?
Setting proxy_set_header Host $host; ensures the backend server receives the original host requested by the client, which is important for virtual hosting and correct response generation.
Click to reveal answer
Which directive forwards the client's IP address to the backend in Nginx?
Aproxy_set_header X-Real-IP $remote_addr;
Bproxy_pass http://backend;
Clisten 80;
Dserver_name example.com;
✗ Incorrect
The directive proxy_set_header X-Real-IP $remote_addr; forwards the client's IP address to the backend server.
What does the header X-Forwarded-For contain?
AThe client's browser type
BOnly the backend server IP
CThe client's IP and all proxy IPs the request passed through
DThe server's hostname
✗ Incorrect
X-Forwarded-For contains the original client IP and all proxy IPs the request passed through.
How do you forward the original protocol (HTTP or HTTPS) in Nginx proxy headers?
Aproxy_pass http://backend;
Bproxy_redirect off;
Clisten 443 ssl;
Dproxy_set_header X-Forwarded-Proto $scheme;
✗ Incorrect
proxy_set_header X-Forwarded-Proto $scheme; forwards the original protocol used by the client.
Why set the Host header in proxy forwarding?
ATo block unwanted hosts
BTo pass the original requested hostname to the backend
CTo change the backend IP
DTo enable SSL
✗ Incorrect
Setting the Host header passes the original hostname requested by the client to the backend.
Which header is NOT typically forwarded by Nginx proxy to preserve client info?
AServer-Timing
BX-Forwarded-For
CX-Forwarded-Proto
DX-Real-IP
✗ Incorrect
Server-Timing is not typically forwarded to preserve client info; it relates to server performance metrics.
Explain why forwarding headers like X-Real-IP and X-Forwarded-For is important in an Nginx proxy setup.
Think about how the backend knows who really made the request.
You got /4 concepts.
Describe how to configure Nginx to forward the original client IP, protocol, and host headers to the backend server.
Focus on the proxy_set_header lines needed.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of using proxy_set_header in an nginx proxy configuration?
easy
A. To cache static files on the proxy server
B. To block client requests based on IP address
C. To compress responses from the backend server
D. To forward original client information like IP and Host to the backend server
Solution
Step 1: Understand proxy_set_header role
The directive proxy_set_header is 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 D
Quick Check:
proxy_set_header forwards client info = D [OK]
Hint: Remember proxy_set_header sends client info to backend [OK]
Common Mistakes:
Thinking it blocks requests
Confusing it with caching or compression
Assuming it modifies client requests directly
2. Which of the following is the correct syntax to forward the original client IP address in nginx proxy configuration?
easy
A. proxy_set_header X-Real-IP $proxy_addr;
B. proxy_pass_header X-Real-IP $remote_addr;
C. proxy_set_header X-Real-IP $remote_addr;
D. proxy_forward_header X-Real-IP $remote_addr;
Solution
Step 1: Identify correct directive for setting headers
The correct directive to set headers in nginx proxy is proxy_set_header.
Step 2: Use correct variable for client IP
The variable $remote_addr holds the original client IP address.
Final Answer:
proxy_set_header X-Real-IP $remote_addr; -> Option C
Quick Check:
proxy_set_header + $remote_addr = B [OK]
Hint: Use proxy_set_header with $remote_addr for client IP [OK]
Common Mistakes:
Using wrong directive like proxy_pass_header
Using $proxy_addr which is invalid
Confusing proxy_set_header with proxy_forward_header
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 B
Quick Check:
$proxy_add_x_forwarded_for appends IP = C [OK]
Hint: Remember $proxy_add_x_forwarded_for appends client IP [OK]
Common Mistakes:
Assuming it replaces the header instead of appending
Confusing $proxy_add_x_forwarded_for with $remote_addr
Expecting only client IP without existing values
4. You configured nginx proxy with:
proxy_set_header X-Forwarded-Proto $scheme;
But the backend always receives http even when client uses https. What is the likely cause?
medium
A. nginx is not configured to listen on HTTPS, so $scheme is always http
B. proxy_set_header syntax is incorrect
C. Backend server ignores X-Forwarded-Proto header
D. You must use $https variable instead of $scheme
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 A
Quick Check:
$scheme depends on nginx listener protocol = A [OK]
Hint: Check if nginx listens on HTTPS for $scheme to be https [OK]
Common Mistakes:
Thinking proxy_set_header syntax is wrong
Assuming backend ignores header without checking
Using $https variable which is not standard
5. You want to forward the original client IP, host, and protocol to your backend server behind nginx proxy. Which complete configuration snippet correctly forwards these headers?
hard
A. proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
B. proxy_set_header X-Real-IP $proxy_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $https;
C. proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $proxy_host;
proxy_set_header X-Forwarded-Proto $scheme;
D. proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $http_scheme;
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.