Tell the Backend Where the Original Request Came From

Understand the proxied Host, client address, and protocol, and configure connection and read timeouts appropriately.

🎯 Introduction

After a request passes through a proxy, the backend's direct connection is to Nginx. When generating links or recording request sources, it may see the wrong hostname, protocol, or address. This article explains how to forward that information and stop waiting when the backend takes too long.


🧱 Request Headers and Trust Boundaries

Request headers carry information about a request. For example, Host identifies the requested site. The proxy creates a new upstream request, so some information must be set explicitly.

In this course, the entry point faces the client directly, with no proxy in front of it. Complete the existing /api/ block as follows:

location ^~ /api/ {
    proxy_pass http://127.0.0.1:9001/;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_connect_timeout 3s;
    proxy_read_timeout 15s;
}

proxy_set_header sets a header in the upstream request. $host is the site hostname determined by Nginx, $remote_addr is the address of the client directly connected to Nginx, and $scheme is http or https for the request to the entry point. Seeing 127.0.0.1 in local tests is expected.

X-Forwarded-For commonly represents the address chain before a request passed through proxies. This example overwrites the client's incoming header and forwards only the actual connecting address, avoiding trust in arbitrary visitor-supplied values. Applications may use these headers only when the request really comes from a trusted proxy.

If you later put a cloud load balancer or CDN (a network service that stores and distributes resources across locations) in front of Nginx, $remote_addr may be its address. At that point, design client address restoration around explicit trusted proxy address ranges. Do not trust arbitrary forwarded headers.


🧪 Make the Mock Backend Echo Received Values

Add these response headers inside the /notes block in mock.conf, keeping the existing X-Backend and X-Seen-URI headers:

add_header X-Seen-Host $http_host;
add_header X-Seen-Proto $http_x_forwarded_proto;
add_header X-Seen-Client $http_x_real_ip;
add_header X-Seen-Forwarded-For $http_x_forwarded_for;

$http_... refers to an incoming request header at the backend, with the header name lowercased and hyphens replaced by underscores. These echoes are for the local exercise only; real applications need not expose internal debugging information.

After reloading, test with a deliberately forged forwarded header:

curl -i -H 'Host: notes.test' -H 'X-Forwarded-For: 203.0.113.9' http://127.0.0.1:8080/api/notes

Expect hostname notes.test, protocol http, and both client and forwarded addresses 127.0.0.1. This shows that the entry point overwrote the forged value. See the official proxy header documentation for setting and inheritance rules.


🧰 What Each Timeout Limits

proxy_connect_timeout 3s limits the wait to establish an upstream connection. proxy_read_timeout 15s limits the interval between successive reads from the upstream; it does not limit the entire request to 15 seconds. If the upstream keeps sending data at shorter intervals, the total request can take longer.

A refused connection often produces 502 Bad Gateway; an upstream timeout often produces 504 Gateway Timeout. Use the error log to establish the cause. Raising a timeout does not speed up a slow query. First find out why the upstream is not responding promptly.

Explicitly setting proxy_http_version 1.1 and clearing Connection prepares for upstream connection reuse in the next configuration. These settings do not automatically add WebSocket support to an ordinary proxy. Real-time bidirectional communication requires additional upgrade handling, which is outside this introductory course.


❓ Questions and Answers

Q1: Can the backend always use X-Real-IP as a user's identity?

No. At most, it records the source within a trusted connection chain. It is not a login identity and cannot replace authorization checks.

Q2: If a request takes more than 15 seconds without an error, has the configuration failed?

Not necessarily. The read timeout measures the interval between reads. A stream that keeps delivering data may never trigger it.


🧪 Exercise

Keep the backend running normally. Request /api/notes with Host: notes.test and then Host: unknown.test, and inspect the echoed headers.

Check your result: both reach the default entry point, but the backend receives the corresponding hostname each time. Explain why default server selection does not automatically replace an unfamiliar Host with server_name. Production applications should still validate allowed domain names when generating absolute links.


🎉 You now know where forwarded request information comes from and when it can be trusted!