Skip to content

Real IP of the users behind reverse proxies and Cloudflare, and Apache log settings - #435

Merged
mistraloz merged 2 commits into
thecodingmachine:v5from
mistraloz:feat/apache-remoteip
Nov 13, 2026
Merged

mistraloz merged 2 commits into
thecodingmachine:v5from
mistraloz:feat/apache-remoteip

Conversation

@mistraloz

Copy link
Copy Markdown
Collaborator

Behind Traefik, nginx, a Kubernetes ingress or Cloudflare, Apache and PHP see the address of the proxy instead of the one of the users. Trusting X-Forwarded-For or CF-Connecting-IP blindly is worse: any client chooses its IP. This PR makes the container find the real IP by itself, without relying on the configuration of the proxy.

environment:
  APACHE_TRUSTED_PROXIES: traefik    # names (resolved every 30 s), IPs or CIDR ranges
  APACHE_TRUSTED_CLOUDFLARE: "1"     # or "only" to refuse the requests sent without Cloudflare
  • Zero trust: mod_remoteip only skips the hops added by the listed proxies. CF-Connecting-IP is only used when the previous hop is a Cloudflare address, even when Traefik or ingress-nginx replaced X-Forwarded-For, so a request sent to the IP of the server cannot forge it.
  • No maintenance: the names of the proxies are resolved again every 30 seconds (restarted or scaled proxy), the Cloudflare ranges are downloaded every day (the copy shipped in the image is the fallback), and Apache is reloaded gracefully when they change.
  • APACHE_TRUSTED_CLOUDFLARE=only: a 403 for the requests of the proxy that did not go through Cloudflare, with the same up-to-date list. No IP list to maintain in Traefik or in a firewall.
  • HTTPS=on for the requests the proxy received in HTTPS, the access log, Require ip and REMOTE_ADDR all get the client IP: the trusted proxies of Symfony or Laravel can stay empty.

Logs, while at it:

  • APACHE_LOG_FORMAT: combined (default), detailed (+ response time and proxy), json, or any format of mod_log_config.
  • APACHE_LOG_OUTPUT=file: files in /var/log/apache2 (a directory of the server or a volume), a new file every day and at APACHE_LOG_MAX_SIZE, gzipped, deleted after APACHE_LOG_RETENTION_DAYS days. stdout stays the default.

The reverse proxy guide (online once merged) builds the configuration of the proxy and of the application for Traefik, nginx or Kubernetes, with or without Cloudflare, in the compose file of the project or in a separate ingress stack, and shows how a legit and a forged request are resolved. It also explains why the trusted proxies of the frameworks are not recommended.

The PR also fixes docker stop of the fpm variant with its built-in Apache when it runs other processes (cron, DAEMON_COMMAND_*, here the watcher of the proxies): bash ignores SIGQUIT in its background jobs, the container was killed after 10 s.

Tested on PHP 8.2 to 8.5, apache and fpm slim images: new apache-trusted-proxies.sh and apache-logs.sh suites, and the existing Apache, fpm, daemon and user suites.

… other processes

With a DAEMON_COMMAND_* or a cron job, supervise.sh sent SIGQUIT to
apache2-fpm-foreground, which bash ignores in its background jobs: the
container was killed after the stop timeout (exit code 137 after 10 s).
SIGTERM stops Apache and PHP-FPM gracefully too.
APACHE_TRUSTED_PROXIES lists the proxies in front of the container: host
names (resolved every 30 seconds), IPs and CIDR ranges.
APACHE_TRUSTED_CLOUDFLARE=1 trusts the ranges of Cloudflare (downloaded
every day, a copy being shipped in the image), and "only" also refuses
the requests that did not go through Cloudflare. mod_remoteip only
trusts the X-Forwarded-For hops added by these proxies, and
CF-Connecting-IP only when the previous hop is a Cloudflare address,
even when the proxy replaced X-Forwarded-For: the container does not
rely on the configuration of the proxy. Requests of a trusted proxy
with X-Forwarded-Proto: https get HTTPS=on.

APACHE_LOG_FORMAT sets the format of the access log (combined, detailed,
json or a format of mod_log_config). APACHE_LOG_OUTPUT=file writes the
logs in /var/log/apache2, rotated every day and at APACHE_LOG_MAX_SIZE
by rotatelogs, gzipped and deleted after APACHE_LOG_RETENTION_DAYS days.
The logs are now set at the server level: the default virtual host no
longer sets its own.

New reverse proxy guide: a configurator for Traefik, nginx, Kubernetes
and Cloudflare, with the path of a request and of a forged one, the
configuration of the proxy and of the application, and the logs.
@mistraloz
mistraloz merged commit 5999cd3 into thecodingmachine:v5 Nov 13, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant