Add opt-in mod_remoteip support for X-Forwarded-For handling #513
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds opt-in Apache
mod_remoteipsupport via theHTTPD_ENABLE_REMOTEIP=1environment variable to properly handleX-Forwarded-Forheaders when running behind reverse proxies, load balancers, or in Kubernetes/OpenShift environments.Why Enable This?
When PHP containers run behind reverse proxies (Nginx, HAProxy, Kubernetes Ingress, OpenShift Routes, cloud load balancers), Apache sees the proxy's IP address instead of the actual client's IP address. This causes issues with:
10.0.0.5) instead of real client IP$_SERVER['REMOTE_ADDR']: Returns proxy IP instead of client IPWhat This Changes
With mod_remoteip Enabled
Apache will:
X-Forwarded-Forheader from trusted proxy IPsREMOTE_ADDRwith the real client IP from the header$_SERVER['REMOTE_ADDR']Example Impact on Logs
Before (proxy IP logged):
After (real client IP logged):
Security Considerations
This feature only trusts
X-Forwarded-Forheaders from private IP ranges:10.0.0.0/8- Private networks172.16.0.0/12- Private networks192.168.0.0/16- Private networks169.254.0.0/16- Link-local127.0.0.0/8- LoopbackThis prevents external clients from spoofing their IP address via forged headers.
Implementation
This implementation follows the approach used in docker-library/wordpress but adapted for RHEL-based httpd (vs Debian's apache2).
Changes
40-remoteip.shfor PHP versions 7.4, 8.0, 8.1, 8.2, 8.3 that conditionally enable mod_remoteipHTTPD_ENABLE_REMOTEIPenvironment variabletest/run-pytestConfiguration Applied
When
HTTPD_ENABLE_REMOTEIP=1is set:Usage
Docker:
Kubernetes/OpenShift:
Test Results
All new tests passing:
🤖 Generated with Claude Code