After enabling reverse proxy, the visitor's source address is not accurate, which request header should be added.
After the site is equipped with a reverse proxy, the visitor address and domain recorded by the backend are incorrect. What request headers need to be supplemented in the proxy layer? The two items to be supplemented are Host and X-Forwarded-For.The document of the proxy module is very clear, by default, the original request's Host and Connection headers will not be passed to the proxy server, so the backend sees the address of the proxy machine and the service name in the proxy configuration;The visitor's source address needs to be restored, which requires appending the client address to X-Forwarded-For and passing the original domain to Host using a variable.
First, see which address the program gets
Troubleshooting starts from the log, not from the configuration. Look at the client address recorded in the backend access log: If all request addresses point to the same internal network machine, it means that the program reads the connection address between the proxy and the program, not the real visitor address.
Similarly, confirm the domain name. If the domain name in the background records is displayed as a service name or proxy domain, rather than the actual domain name visited by the visitor, it means that Host did not pass the original request correctly. These two phenomena often occur together, but the causes are two separate configurations, which are handled separately.
What fields are not passed by default?
The default agent module does not pass through the original Host nor Connection. The consequence of the former is that the backend does not know which domain the visitor's request is directed to, which is particularly critical in multi-site domain分流;the default handling of the latter is actually a good thing, because the reuse and shutdown signals of the connection should be managed by the agent layer itself.
It should be noted that there are two ways to set the value of Host. One way is to pass the Host header from the client's request as is, but if there is no such header in the request, nothing will be passed through; for this reason, the document suggests using the $host variable - its value is equal to the service name in the request header when there is one, and falls back to the main service name in the configuration when missing. For public websites, the latter is more stable.
| Request Headers | Default whether to pass through | The phenomenon after missing | Key points of writing in the proxy layer |
|---|---|---|---|
| Host | Not passing through | The backend recognizes the wrong domain name, causing multiple sites to jump to the wrong site | Pass the original domain name through a variable, and fall back when missing |
| X-Forwarded-For | Set manually | Visitor addresses are all recorded as proxy machines | Append the client address to the existing list |
| Connection | Not passing through | Generally do not need to care | Managed by the proxy layer |
| Protocol identifier | Set manually | HTTPS is identified as HTTP, causing a redirect loop | Maintain consistency with the certificate offloading method |
Writing in two-line configuration
The meaning of the combination variable provided by the module: $proxy_add_x_forwarded_for is to append the client address with a comma to the existing X-Forwarded-For in the request. If the request does not have this header originally, the value is equal to the client address itself. This design is crucial for multi-level proxies - directly overriding would discard the address chain already written by the upstream, appending is the only way to retain the complete chain.
The way to write the host follows the suggestions above, use a variable to pass the original domain name. After both are configured, the backend will read the visitor's source address, original domain name, and protocol, and the logic of on-site visit statistics, comment records, and domain-based routing will be consistent.
One more thing to remind you: Do not trust the X-Forwarded-For from the visitor side. When there are multiple entry points, only the appended values on the trusted proxy chain are valid, and the forged values carried by external requests must be overridden or cleaned up at the outermost entry point, otherwise the source address statistics can be manipulated manually.
What to pay attention to when multiple sites share a single entry point.
Under the multi-site scenario, the role of Host is amplified. AnQiCMS supports the management of multiple independent sites on the same backend, suitable for multi-brand and multi-theme websites. The same service often distributes requests to different sites based on domain names. At this time, if Host is missing or passed as a proxy name, it may lead to misrecognition of sites and log confusion, or even route visitors to the wrong brand site.
Port mapping should also be recorded. AnQiCMS listens to port 8001 by default, supports deployment methods such as Docker image, Baota panel, and regular environment deployment, and the proxy layer points to this service port. It is recommended to clarify three things in the deployment documentation: the external entry port, the target port of the proxy forwarding, and where the certificate is unloaded. If any of these are missing, the fault will manifest as an alternating appearance of 'the background cannot see the real source' and 'jumping loop', which is very costly to troubleshoot.
Reverse proxy itself has another benefit: static resources and page output are cached at the proxy layer, which can significantly alleviate the response pressure under high concurrency. The page loading speed of AnQiCMS is significantly improved compared to traditional PHP CMS, the official figure is that a single machine can handle about 5 million PV of traffic, and after adding proxy caching, the amount of requests the source station needs to handle can still be further reduced.
Common questions
What if we only supplement X-Forwarded-For but do not supplement Host?The visitor's source address is correct, but the backend still does not know the original domain name. The multi-site distribution according to the domain name will fail, and the site ownership in the logs is also not reliable.
Can the backend parse this header directly?It can be read, but the scope of trusted proxy should be limited. Trusting the first address in the header without judgment is equivalent to giving the source address to the visitor to fill in.
Why does the HTTPS site appear to have a redirect loop?It is usually because the certificate is unloaded at the proxy layer, and when forwarding to the backend, the protocol information is not transmitted together, causing the backend to think that it receives a plaintext request and forces a redirect. Simply add the protocol identifier.
Do I need to restart after changing the configuration?After modifying the proxy configuration, reload the entry service according to the normal process, and then check the logs to confirm that both the address and domain have been restored.