@@ -345,3 +345,76 @@ $app = new FrameworkX\App($container);
345
345
346
346
$app->run();
347
347
```
348
+
349
+ X supports running behind reverse proxies just fine. However, by default it will
350
+ see the IP address of the last proxy server as the client IP address (this will
351
+ often be ` 127.0.0.1 ` ). You can get the original client IP address if you configure
352
+ your proxy server to forward the original client IP address in the ` X-Forwarded-For `
353
+ (XFF) or ` Forwarded ` HTTP request header. If you want to use these trusted headers,
354
+ you may use a custom middleware to read the IP from this header before passing
355
+ it to the [ ` AccessLogHandler ` ] ( middleware.md#accessloghandler ) like this:
356
+
357
+ === "Using middleware instances"
358
+
359
+ ```php title="public/index.php"
360
+ <?php
361
+
362
+ use Acme\Todo\TrustedProxyMiddleware;
363
+
364
+ require __DIR__ . '/../vendor/autoload.php';
365
+
366
+ $app = new FrameworkX\App(
367
+ new TrustedProxyMiddleware(),
368
+ new FrameworkX\AccessLogHandler(),
369
+ new FrameworkX\ErrorHandler()
370
+ );
371
+
372
+ // Register routes here, see routing…
373
+
374
+ $app->run();
375
+ ```
376
+
377
+ === "Using middleware names"
378
+
379
+ ```php title="public/index.php"
380
+ <?php
381
+
382
+ use Acme\Todo\TrustedProxyMiddleware;
383
+
384
+ require __DIR__ . '/../vendor/autoload.php';
385
+
386
+ $app = new FrameworkX\App(
387
+ TrustedProxyMiddleware::class,
388
+ FrameworkX\AccessLogHandler::class,
389
+ FrameworkX\ErrorHandler::class
390
+ );
391
+
392
+ // Register routes here, see routing…
393
+
394
+ $app->run();
395
+ ```
396
+
397
+ ``` php title="src/TrustedProxyMiddleware.php"
398
+ <?php
399
+
400
+ namespace Acme\Todo;
401
+
402
+ use Psr\Http\Message\ServerRequestInterface;
403
+
404
+ class TrustedProxyMiddleware
405
+ {
406
+ public function __invoke(ServerRequestInterface $request, callable $next)
407
+ {
408
+ // use 127.0.0.1 as trusted proxy to read from X-Forwarded-For (XFF)
409
+ $remote_addr = $request->getAttribute('remote_addr') ?? $request->getServerParams()['REMOTE_ADDR'] ?? null;
410
+ if ($remote_addr === '127.0.0.1' && $request->hasHeader('X-Forwarded-For')) {
411
+ $remote_addr = preg_replace('/,.*/', '', $request->getHeaderLine('X-Forwarded-For'));
412
+ $request = $request->withAttribute('remote_addr', $remote_addr);
413
+ }
414
+
415
+ return $next($request);
416
+ }
417
+ }
418
+ ```
419
+
420
+ See also [ middleware handling] ( middleware.md ) for more details.
0 commit comments