Hello, I'm not very familiar with PHP syntax. I tried the following code based on AI suggestions:
use Flarum\Extend;
return [
(new Extend\ServiceProvider())
->register(function ($container) {
$trustedProxies = ['192.168.1.1', '127.0.0.1'];
if (in_array($_SERVER['REMOTE_ADDR'], $trustedProxies)) {
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = trim($ips[0]);
} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];
}
}
})
];
This code causes my forum to throw a 500 error. Later, I simplified the code as follows, but it still produces the same error:
use Flarum\Extend;
return [
(new Extend\ServiceProvider())
->register(function ($container) {
if (isset($_SERVER['HTTP_X_REAL_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];
}
})
];
I couldn't find detailed error information from either Caddy or PHP. However, based on your hint, I added the following code to index.php and it achieved my requirement.
I would really appreciate it if you could share any better implementation methods.
if (isset($_SERVER['HTTP_X_REAL_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$forwardedIps = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = trim($forwardedIps[0]);
}