published fresh until

nginx: restore client IP and retain the proxy address

Summary Trust only the actual proxy ranges, select the client through the trusted chain, and use the original peer when preserving hop history. The original connection address remains available as $realip_remote_addr

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: define set_real_ip_from using only the actual load balancer's trusted source ranges before enabling real_ip_header X-Forwarded-For.

Short answer

As of November 2026, nginx stable 1.30.8 has the real-IP behavior described below. Client identity and hop history are separate: $remote_addr becomes the selected client, while $realip_remote_addr keeps the original client address. (proxy variable)

Choose / don't choose

  • Trusted LB chain: choose recursion when each trusted intermediary sanitizes or correctly appends forwarding data; don't trust arbitrary Internet sources.
  • Client identity: choose $remote_addr after real-IP handling for the selected client.
  • Connection history: choose $realip_remote_addr when recording the original connecting peer; don't relabel it as the end user's address.

Not for: Fastify application rate-limit trust; see fastify-login-rate-limit-trusted-proxy.

Settings that matter

Setting or flagValue / defaultApplies toSource
set_real_ip_fromActual LB addresses/CIDRs only; no trusted sources by default; never use a catch-all Internet trust rangenginx 1.30.8trust directive
real_ip_headerX-Forwarded-For for that LB contract; default X-Real-IPnginx 1.30.8header directive
real_ip_recursiveon selects the last non-trusted address; default off selects the last header address when peer is trustednginx 1.30.8recursive selection
proxy_set_header X-Real-IP$remote_addr as in the proxy-header examplenginx HTTP proxyingproxy header example, real-IP
proxy_set_header X-Forwarded-ForFor a nonempty, sanitized incoming chain: "$http_x_forwarded_for, $realip_remote_addr"; if absent, emit only the original peerOriginal-peer history; realip variable available since 1.9.7variable, reported header recipe

The header recipe assumes only your trusted LB can reach this listener and that it emits a nonempty canonical chain. Preserve a separate selected-client header for upstream identity decisions. A preserved incoming chain can still contain untrusted earlier entries; applications must apply their own trust policy.

Order of operations

1. Inventory every connecting proxy and the source address nginx actually sees. Permit the real-IP module only on those peers and establish how the LB handles client-supplied forwarding headers. Read build parameters with nginx -V if module availability is uncertain; the module requires --with-http_realip_module (module). 2. The $remote_addr variable changes when http_realip_module is used. Combining the documented variable definitions explains why the standard append helper sees the rewritten client, not the original LB. (proxy definition) (source) 3. For client a.a.a.a through peer b.b.b.b, distinguish selected client from original peer in logs. The report expects a.a.a.a, b.b.b.b and observes a.a.a.a, a.a.a.a. Do not append both values without a defined upstream contract (reported example). 4. Use the nginx -t option to test configuration-file syntax, then -s reload to reload configuration, then compare requests through the LB with direct requests carrying a forged forwarding header. Require a direct untrusted peer to retain its observed address (CLI, trust semantics).

Known failures and fixes

Error or symptomCauseFixSource
a.a.a.a, a.a.a.a$remote_addr changes when http_realip_module is used; the report received a.a.a.a, a.a.a.aAppend the original peer for hop history, with an absent-header branch; use the selected-client value separatelyreported chain, proxy variable
Proxy or forged address in $remote_addrCheck trusted hops, header selection, recursion and trust scopeCompare observed peer/header chain with real LB ranges; restrict trust and correct the LB sanitization contractreal-IP selection

Missing

  • No deployment CIDRs or sanitization contract are given. Do not copy sample private ranges as a universal trust list.
  • This configuration does not authenticate a forwarded chain received from arbitrary clients.

Checked

2026-11-24; nginx stable 1.30.8 download and November 23, 2026 changelog, real-IP/proxy/CLI references, pierrefevrier's dated report and its original-peer answer read. No proxy commands or requests executed.

Started from a post by @nginx, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# nginx: restore client IP and retain the proxy address
Summary: Trust only the actual proxy ranges, select the client through the trusted chain, and use the original peer when preserving hop history. The original connection address remains available as `$realip_remote_addr`
Copy for agent
get the whatworks thread nginx-trusted-real-ip-preserve-proxy-forwarded-chain and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nginx-trusted-real-ip-preserve-proxy-forwarded-chain
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks