REMOTE_ADDR IP from user instead of Nginx reverse proxy server

REMOTE_ADDR IP from user instead of Nginx reverse proxy server

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why REMOTE_ADDR Returns the Proxy IP, and How to Fix It

When you run a PHP application behind an Nginx reverse proxy, the default behavior of $_SERVER['REMOTE_ADDR'] is often frustrating. The function returns the IP address of the proxy server (Nginx) rather than the actual client connecting to your backend. This happens because every request passes through the proxy first, making the proxy appear as the direct sender in the raw network handshake.

While Nginx can be configured to forward the client's original IP via the X-Forwarded-For header, PHP does not automatically read this header into REMOTE_ADDR. To get the true user IP, you must explicitly tell your web server how to handle these headers. This guide covers the standard approach using Apache's mod_remoteip and the legacy alternative rpaf, addressing common configuration pitfalls.

The Role of mod_remoteip in Modern Apache

Apache 2.4 introduced a robust solution via the mod_remoteip module. If you are running Apache 2.4.10 or later, this is the preferred method over older modules like rpaf. It provides a cleaner API and better performance characteristics for handling reverse proxy scenarios.

To enable this, you must ensure the module is loaded. Check your Apache configuration files (usually in /etc/apache2/mods-available/ on Debian-based systems) to confirm remoteip.conf exists and contains LoadModule remoteip_module modules/libhttpd/mod_remoteip.so. If the line is commented out or missing, uncomment it and restart the service:

sudo a2enmod remoteip
sudo systemctl restart apache2

Once loaded, you need to configure how Apache interprets the incoming headers. Create or edit /etc/apache2/conf-available/remoteip.conf (or place the directives in your main httpd.conf) with the following:

RemoteIPHeader X-Forwarded-For
RemoteIPTrustedProxy 172.19.0.5

In this example, 172.19.0.5 is the IP address of your Nginx proxy. By setting RemoteIPTrustedProxy, you instruct Apache to trust that any request coming from that specific IP should have its source IP replaced with the value found in the X-Forwarded-For header.

Configuring Nginx to Send the Correct Headers

Apache alone cannot solve this; the upstream proxy must send the data. Your Nginx configuration needs to populate the X-Forwarded-For and X-Real-Ip headers correctly before passing traffic to Apache.

In your Nginx server block, ensure the following directives are present within the location or server context:

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://172.19.0.4;
}

The directive proxy_add_x_forwarded_for is crucial here. It appends the current client's IP to the existing header value, preserving a chain of IPs if multiple proxies are involved. Without this, you risk losing historical context about how the request traversed your infrastructure.

The Legacy rpaf Module and Its Limitations

If you are maintaining older systems or cannot upgrade Apache, the rpaf_module (Remote Port Address Forwarding) is a common alternative. As noted in recent troubleshooting threads, users often install this module and see it listed in phpinfo(), yet it fails to update REMOTE_ADDR.

This usually stems from incorrect configuration syntax or file paths. The standard configuration file is typically located at /etc/apache2/mods-available/rpaf.conf. A functional configuration looks like this:

<IfModule rpaf_module>
    RPAFenable On
    RPAFsethostname On
    RPAFproxy_ips 172.19.0.5
    RPAFheader X-Forwarded-For
</IfModule>

A frequent mistake is omitting the <IfModule> directive or misspelling the configuration keys (e.g., RPAFsethostname vs RPAFSetHostname). Apache is case-sensitive regarding some directives, and strict validation is required. Additionally, ensure that rpaf.conf is enabled via a2enmod rpaf and that the module version matches your PHP handler capabilities.

Handling Multiple Proxies and Security Considerations

When deploying in complex environments where requests might pass through multiple layers (e.g., Load Balancer -> Nginx -> Apache), relying solely on X-Real-Ip can be risky. The X-Forwarded-For header is an array of IPs; the first entry is usually the original client, while subsequent entries represent intermediate proxies.

Modern frameworks and PHP applications should prioritize parsing the X-Forwarded-For header directly if available, falling back to REMOTE_ADDR. However, relying on server-side configuration like mod_remoteip or rpaf simplifies this by automating the extraction of the first IP in the chain.

Be cautious when trusting these headers. If your application is exposed to untrusted networks without proper proxy validation, a malicious actor could spoof the X-Forwarded-For header to hide their real IP. Always ensure that only trusted proxy IPs are listed in your RemoteIPTrustedProxy or RPAFproxy_ips directives.

For developers seeking further guidance on securing reverse proxy setups and handling IP forwarding in PHP applications, resources like https://laravelcompany.com offer additional insights into modern framework configurations.

Our network