When several sites share a dedicated server, the reverse proxy often handles access logging, rate limiting, and source-IP allowlists. If the backend blindly trusts a client-supplied X-Forwarded-For header, even an ordinary request can spoof its source address. This article builds a two-layer loopback lab on Debian 12 to test normal forwarding, forged headers, and direct backend access without changing existing site configurations or exposing a public listener.
1. Define the Address Trust Boundary First
Distinguish between two addresses: the peer that establishes the connection directly, and the client address resolved through a trusted proxy. The former comes from the connection; the latter may come from a request header. Only when the immediate peer is a trusted proxy should the resolved client address be allowed to replace the connection address.
The lab's ingress proxy listens on 127.0.0.1:18080, and the backend listens on 127.0.0.1:18081. The ingress proxy binds its outgoing connection to 127.0.0.2, which is the only address the backend trusts. These ports are arbitrary choices for the lab, not delivery defaults.
At the public ingress, use an overwrite policy: discard incoming forwarding headers and write the actual connection address instead. This article does not append addresses, to avoid mixing client-supplied values into the trusted chain. With multiple proxy layers, define explicitly, hop by hop, which proxy overwrites and which appends. Do not simply copy a single-layer configuration.
Request headers are not proof of identity. An overly broad trusted-proxy list, or an application that bypasses the proxy's interpretation and parses raw headers again, can invalidate checks at the ingress layer.
Other local processes can also use loopback source addresses. This lab therefore tests address-resolution rules; it does not provide identity isolation between local users. Production deployments also need network boundaries and process permissions working together.
2. Start an Isolated Two-Layer Lab
Use an existing installation of nginx and curl. Run nginx -V and confirm that it includes --with-http_realip_module. If the module is missing, install a package that provides it first. Do not bypass the check by deleting the real-IP configuration.
Run the following commands as an unprivileged user. A separate prefix directory holds the configuration, logs, and PID, leaving the system service untouched. If either listening port is already in use, stop the lab setup, choose unused ports, and update every test command accordingly.
LAB=$(mktemp -d /tmp/ip-trust.XXXXXX)
mkdir -p "$LAB/logs"
cat > "$LAB/nginx.conf" <<'EOF'
worker_processes 1;
pid logs/nginx.pid;
error_log logs/error.log notice;
events {
worker_connections 128;
}
http {
log_format evidence '$server_port client=$remote_addr '
'peer=$realip_remote_addr '
'xff="$http_x_forwarded_for"';
access_log logs/access.log evidence;
default_type text/plain;
server {
listen 127.0.0.1:18080;
location / {
proxy_bind 127.0.0.2;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP "";
proxy_set_header Forwarded "";
proxy_pass http://127.0.0.1:18081;
}
}
server {
listen 127.0.0.1:18081;
set_real_ip_from 127.0.0.2/32;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
location / {
return 200 "$remote_addr|$realip_remote_addr\n";
}
}
}
EOF
nginx -p "$LAB/" -c "$LAB/nginx.conf" -t
nginx -p "$LAB/" -c "$LAB/nginx.conf"
Run the start command only after the syntax check succeeds. Then use ss -ltn to verify the listening addresses. Both must be 127.0.0.1; neither may be a wildcard address.
The left side of the response is the resolved client address; the right side is the original connection peer. The log also records the received forwarding header so you can compare all three values. The lab explicitly clears two other common source-address headers to prevent a downstream application from mistakenly using a different field. This does not mean that every application-specific header has been covered.
3. Test Three Request Paths to Verify Spoofing Fails
Normal Ingress and Forged Request Headers
First, force the client to bind to 127.0.0.1 so source-address selection does not affect the results. --noproxy '*' prevents proxy environment variables in the current shell from routing lab requests elsewhere.
curl --noproxy '*' --interface 127.0.0.1 \
http://127.0.0.1:18080/
curl --noproxy '*' --interface 127.0.0.1 \
-H 'X-Forwarded-For: 198.51.100.77, 203.0.113.88' \
-H 'X-Real-IP: 198.51.100.77' \
-H 'Forwarded: for=198.51.100.77' \
http://127.0.0.1:18080/
Both requests should return 127.0.0.1|127.0.0.2. The documentation-example addresses in the second request must not become the client address: the ingress has overwritten the forwarding header, and the trusted connection peer seen by the backend is still 127.0.0.2.
If the results differ, first check that the requests reached the isolated lab process. Then inspect the configuration actually in use and the ingress directives that overwrite the headers. Do not start by broadening the trusted subnet.
Bypass the Ingress and Connect Directly to the Backend
curl --noproxy '*' --interface 127.0.0.3 \
-H 'X-Forwarded-For: 198.51.100.77' \
http://127.0.0.1:18081/
cat "$LAB/logs/access.log"
The expected response is 127.0.0.3|127.0.0.3. Although the backend receives a forged header, the immediate connection peer is not on the trusted list, so the address must not be rewritten. The raw xff field in the log may still contain the forged value. That field is evidence for investigation, not a basis for authorization.
This test proves only that an untrusted peer cannot rewrite the address. It does not prove that the backend cannot be reached by bypassing the ingress. These two acceptance checks must be completed separately.
4. Map the Lab Rules to the Production Proxy Chain
Before configuring production, map the connection sequence: external client, ingress proxy, internal proxy, application. At each hop, record the actual outbound address and the connection address seen by the backend. After address translation, these may differ from the device's management address.
Limit trust to the proxies' actual egress addresses. Do not add all sources, an entire internal subnet, or all loopback addresses to the trusted list. If the proxy and application run on the same host, also check whether other local accounts can connect to the backend. Listening on loopback is not authentication.
The application layer must use a single, consistent method for resolving source addresses. If the framework also has “trust all proxies” enabled, the application's allowlist may use the wrong address even when ingress logs are correct. Cross-check rate limiting, auditing, and access control for the same request rather than inspecting just one log.
Multiple separate IPv4 addresses do not automatically create a trust chain either. When a HttpAmd dedicated server has multiple addresses, management connections use the first address. The service's listening address and the addresses trusted as proxies are two separate settings that must be recorded and verified independently.
If the production backend is intended only for proxy access, restrict which sources can reach it. If a direct health-check path must remain available, explicitly identify the checker. Do not trust an entire source range merely to make probing convenient.
5. Preserve Evidence and Verify the Scope of the Change
Before going live, save the three requests, their corresponding logs, and the effective configuration. Record the expected client address and immediate peer for each path. Where evidence contains real user addresses, store it under the appropriate business access controls; do not publish raw access logs.
Acceptance must cover at least four checks: normal requests work; forged headers cannot change the identified address; untrusted direct connections cannot rewrite the address; and unauthorized sources cannot bypass the ingress to reach the production backend. If the application also handles IPv6, add separate address and proxy-chain samples. Do not infer IPv6 behavior from this IPv4 lab.
Even when only the proxy configuration changes, check whether application logs and rate-limit keys change as a result. If problems occur, first roll back the source-address configuration changes made in this update and retain the failed requests and logs. Do not eliminate errors by expanding the scope of trust.
When testing is complete, stop the process using the lab's separate PID:
nginx -p "$LAB/" -c "$LAB/nginx.conf" -s quit
Confirm that both lab listeners have disappeared before removing the temporary directory. The final deliverable is not a statement that “real IP is enabled.” It is a set of repeatable requests, backed by evidence that every path follows the trust rules.
Frequently asked questions
Is the leftmost X-Forwarded-For address safe to use directly?
No. First establish that the immediate peer is an explicitly trusted proxy. The public ingress must overwrite forwarding headers supplied by clients.
Can the lab's loopback addresses be copied into production trust settings?
Not unchanged. They are local test peers. Production must trust the actual proxy egress addresses and restrict direct access to the backend.
AMD EPYC 7543P Dedicated Server
HttpAmd From $470.5/month