Content · Web Development · PHP

Using a Reverse Proxy with PHP

Reverse proxies terminate TLS or route requests to PHP services behind a trusted layer. Only honor forwarding headers from explicitly trusted proxy.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Reverse proxies terminate TLS or route requests to PHP services behind a trusted layer.

What It Does


Reverse proxies terminate TLS or route requests to PHP services behind a trusted layer. The following examples distinguish a normal use case, a boundary case and a practical application.

Requirements and Compatibility


PHP 8.x and a local test environment. Apply input validation, correct error handling and the requirements of any optional libraries.

The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.

Example 1: Basic Behavior


Understand the primary operation and its inputs.

<?php
$trusted = ['10.0.0.10'];
$peer = '10.0.0.10';
$forwarded = '198.51.100.11';
$ip = in_array($peer, $trusted, true) ? $forwarded : $peer;
echo $ip;

Example 2: Another Case


Check failure behavior or a different execution path.

<?php
$peer = '203.0.113.7';
$header = '127.0.0.1';
$ip = $peer; // Ignore X-Forwarded-For from an untrusted peer
echo $ip;

Practical Example: Putting It to Use


Only trust forwarded scheme and host metadata from your configured reverse proxy.

<?php
$trustedProxy = '10.0.0.5'; $directPeer = '203.0.113.12';
$forwardedProto = 'https';
$scheme = $directPeer === $trustedProxy ? $forwardedProto : 'http';
echo $scheme;

Important Behavior and Edge Cases


Only honor forwarding headers from explicitly trusted proxy addresses.

Comparison with Related Functions or Approaches


OptionWhat it means in practice
Using a Reverse Proxy with PHPReverse proxies terminate TLS or route requests to PHP services behind a trusted layer.
Related approachSet correct trusted hosts and proxy policies in web-server and application configuration.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the important boundary for Using a Reverse Proxy with PHPOnly honor forwarding headers from explicitly trusted proxy addresses.
Assuming a related mechanism has the same behaviorSet correct trusted hosts and proxy policies in web-server and application configuration.

Key Takeaways


  • Reverse proxies terminate TLS or route requests to PHP services behind a trusted layer.
  • Only honor forwarding headers from explicitly trusted proxy addresses.
  • Set correct trusted hosts and proxy policies in web-server and application configuration.

Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.

Share this resourceSend this page to someone who may find it useful.