Why are all the client addresses logged in the log all proxy addresses when a proxy is hung in front of the website?
After setting up the reverse proxy, the client addresses seen in the access logs and statistics are all the same internal network or proxy address. This is not a mistake in the log: From the perspective of the program, this TCP connection indeed comes from the proxy. The real source can only be obtained from the forwarding header, and the reliability of this header needs to be specially handled. Taking it wrong is more麻烦 than not taking it.
What does the left and right order of the address list mean
The header contains a comma-separated list of addresses. According to the document definition, the one on the right is the nearest proxy, and the one on the left claims to be the original client. With each layer of proxy, a layer is appended to the end, so the list reads from left to right as 'from the claimed source to the nearest forwarder', and the credibility increases from left to right.
This means that the two methods need to be separated: to restore the visitor's address, take the one on the leftmost side of the list; to find out who connected at this hop, take the direct address instead of the content in the header.
Why can't it be received as a whole string?
Because the client can also write this header. If the program directly trusts the entire list, an attacker can pre-load any address, causing all subsequent judgments to be false. The boundary given in the document is very direct: as long as the server can be directly connected to the public network, then no part of the list can be considered trustworthy or used for security-related purposes.
| Purpose | Can we use the forwarding header? | Value range |
|---|---|---|
| Traffic and regional statistics | Can be used, tolerate error | Take the leftmost, allow empty |
| Interface speed limit | Only trust the addresses appended by the trusted proxy | 剥到已知代理的右边 |
| Ban and access control by address | The same, must be strict | The same |
| Audit and traceability | Need to align with gateway logs | Cross-check of two-level records |
Which uses can be trusted and which cannot
Statistical errors in usage affect reports and can be tolerated to a certain extent. Judgments made based on addresses for limiting speed, blocking, and preventing collection can backfire once they are incorrect: treating proxy addresses as visitors means that all site visitors share a quota; treating forged addresses as visitors means that attackers decide who to block for themselves. Therefore, the rule at this level is the same - only use the part added by trusted proxies. The method is to peel off known proxy and load balancing addresses from right to left, and the first position not on the trusted list is the actual source that is truly connected.
Three steps align on the deployment side.
The first step is to fix the link: confirm that the site can only be accessed through a proxy, the source site should not be directly exposed to the public network, otherwise the above可信 premise does not hold. The second step is to configure the trusted proxy range at the deployment layer, both Baota panel and self-built Nginx need to write in the proxy segment address, so that the program knows from which layer to start taking values; when deploying with Docker image, this layer of configuration needs to be brought in as well, and not just change the gateway. The third step is to make the logging and statistics口径 consistent: the fields used when writing and the fields used when reading must be the same, otherwise historical data cannot be compared.
When migrating data and restoring backups, this layer also needs to be checked. The backup includes static files and configurations, and after the restore, if the configuration of the trusted proxy does not come back, the logs will revert to full-screen proxy addresses, and access statistics and address-based protection will be distorted at the same time.
Common questions
Question: Why not use the real IP extension header directly?Custom headers only make sense when the link between the proxy and the origin server is completely controllable. The general practice is still to handle standard forwarding headers according to the list of trusted proxies.
Question: Will using a proxy or browser plugin by visitors cause the address to be wrong?Will. The one on the leftmost of the list is the claimed source, which may be rewritten by the client, statistics can be accepted, but security judgment cannot be accepted.
Question: Should IPv6 addresses be treated specially?Need. The trusted list and stripping logic must cover both protocol families at the same time, only writing one part will make the other part fall into the untrusted branch.