feat: Add Real IP forwarding feature for bare-metal clusters
All checks were successful
Helm Chart Release / release-chart (push) Successful in 12s

ベアメタルKubernetesクラスターやLoadBalancer環境において、
PHP側で訪問者の実IPアドレスを取得できる機能を追加。

Changes:
- Add nginx.forwardRealIP configuration in values.yaml
- Implement real_ip_header and set_real_ip_from in Nginx config
- Pass real IP info to PHP-FPM via fastcgi_param
- Add usage example and documentation in README.md
- Create test-real-ip.php for verification
- Update chart version to 8.5.2-a

Features:
- Compatible with existing customConfig.snippet
- Configurable trusted proxy networks
- Supports multi-tier proxy with recursive option
- Default disabled for backward compatibility

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-10 09:21:55 +09:00
parent 59a21fca9b
commit 02696fc55e
6 changed files with 434 additions and 3 deletions

226
examples/test-real-ip.php Normal file
View File

@@ -0,0 +1,226 @@
<?php
/**
* Real IP Test Script
*
* このスクリプトは、NginxのリアルIP転送設定が正しく動作しているかを確認します。
*
* 使用方法:
* 1. values.yamlでnginx.forwardRealIP.enabled=trueに設定
* 2. このファイルを /var/www/html/ に配置
* 3. ブラウザでアクセス: http://your-service/test-real-ip.php
*/
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Real IP Test</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
border-bottom: 3px solid #4CAF50;
padding-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #4CAF50;
color: white;
font-weight: bold;
}
tr:hover {
background-color: #f5f5f5;
}
.success {
color: #4CAF50;
font-weight: bold;
}
.warning {
color: #ff9800;
font-weight: bold;
}
.info {
background: #e3f2fd;
padding: 15px;
border-left: 4px solid #2196F3;
margin: 20px 0;
}
code {
background: #f4f4f4;
padding: 2px 6px;
border-radius: 3px;
font-family: 'Courier New', monospace;
}
</style>
</head>
<body>
<div class="container">
<h1>🔍 Real IP Detection Test</h1>
<div class="info">
<strong> 情報:</strong> このページは、NginxのリアルIP転送設定が正しく動作しているかを確認します。
</div>
<h2>📊 IP Address Information</h2>
<table>
<tr>
<th>変数名</th>
<th>値</th>
<th>説明</th>
</tr>
<tr>
<td><code>REMOTE_ADDR</code></td>
<td class="success"><?php echo htmlspecialchars($_SERVER['REMOTE_ADDR'] ?? 'not set'); ?></td>
<td>クライアントの実IPアドレスNginx real_ip設定適用後</td>
</tr>
<tr>
<td><code>HTTP_X_REAL_IP</code></td>
<td><?php echo htmlspecialchars($_SERVER['HTTP_X_REAL_IP'] ?? 'not set'); ?></td>
<td>Nginxが設定したリアルIP互換性用</td>
</tr>
<tr>
<td><code>HTTP_X_FORWARDED_FOR</code></td>
<td><?php echo htmlspecialchars($_SERVER['HTTP_X_FORWARDED_FOR'] ?? 'not set'); ?></td>
<td>プロキシチェーン全体(カンマ区切り)</td>
</tr>
<tr>
<td><code>HTTP_CLIENT_IP</code></td>
<td><?php echo htmlspecialchars($_SERVER['HTTP_CLIENT_IP'] ?? 'not set'); ?></td>
<td>一部のプロキシが使用</td>
</tr>
</table>
<h2>🔧 All HTTP Headers</h2>
<table>
<tr>
<th>Header Name</th>
<th>Value</th>
</tr>
<?php
$headers = [];
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$headerName = str_replace('_', '-', substr($key, 5));
$headers[$headerName] = $value;
}
}
ksort($headers);
foreach ($headers as $name => $value) {
echo '<tr>';
echo '<td><code>' . htmlspecialchars($name) . '</code></td>';
echo '<td>' . htmlspecialchars($value) . '</td>';
echo '</tr>';
}
?>
</table>
<h2>✅ 診断結果</h2>
<div class="info">
<?php
$remoteAddr = $_SERVER['REMOTE_ADDR'] ?? '';
$xForwardedFor = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? '';
// プライベートIPかどうかをチェック
function isPrivateIP($ip) {
$privateRanges = [
'10.0.0.0|10.255.255.255',
'172.16.0.0|172.31.255.255',
'192.168.0.0|192.168.255.255',
'127.0.0.0|127.255.255.255'
];
$longIP = ip2long($ip);
foreach ($privateRanges as $range) {
list($start, $end) = explode('|', $range);
if ($longIP >= ip2long($start) && $longIP <= ip2long($end)) {
return true;
}
}
return false;
}
if (!empty($remoteAddr) && !isPrivateIP($remoteAddr)) {
echo '<p class="success">✅ <strong>正常:</strong> REMOTE_ADDR にパブリックIPアドレスが設定されています。</p>';
echo '<p>リアルIP転送設定が正しく動作しています。</p>';
} elseif (!empty($remoteAddr) && isPrivateIP($remoteAddr)) {
echo '<p class="warning">⚠️ <strong>注意:</strong> REMOTE_ADDR がプライベートIPアドレスです: ' . htmlspecialchars($remoteAddr) . '</p>';
echo '<p>これは以下のいずれかが原因です:</p>';
echo '<ul>';
echo '<li>リアルIP転送設定nginx.forwardRealIP.enabledが無効</li>';
echo '<li>trustedProxiesの設定が不適切</li>';
echo '<li>LoadBalancerがX-Forwarded-Forヘッダーを送信していない</li>';
echo '</ul>';
if (!empty($xForwardedFor)) {
echo '<p><strong>ヒント:</strong> X-Forwarded-For ヘッダーには値があります: ' . htmlspecialchars($xForwardedFor) . '</p>';
echo '<p>values.yaml で <code>nginx.forwardRealIP.enabled: true</code> に設定してください。</p>';
}
} else {
echo '<p class="warning">⚠️ REMOTE_ADDR が設定されていません</p>';
}
?>
</div>
<h2>📝 PHPでの使用例</h2>
<pre style="background: #f4f4f4; padding: 15px; border-radius: 5px; overflow-x: auto;">
<code>&lt;?php
/**
* 訪問者のリアルIPアドレスを取得する関数
*/
function getRealIP() {
// nginx.forwardRealIP.enabled が true の場合、
// REMOTE_ADDR に実IPが設定されている
return $_SERVER['REMOTE_ADDR'] ?? 'unknown';
}
// 使用例
$visitorIP = getRealIP();
echo "訪問者のIPアドレス: " . $visitorIP;
// データベースに記録する例
// $stmt = $pdo-&gt;prepare("INSERT INTO access_log (ip_address, visited_at) VALUES (?, NOW())");
// $stmt-&gt;execute([$visitorIP]);
?&gt;</code></pre>
<h2>⚙️ Helm設定例</h2>
<pre style="background: #f4f4f4; padding: 15px; border-radius: 5px; overflow-x: auto;">
<code># values.yaml
nginx:
forwardRealIP:
enabled: true
header: "X-Forwarded-For"
recursive: true
trustedProxies:
- "10.0.0.0/8"
- "172.16.0.0/12"
- "192.168.0.0/16"
additionalTrustedProxies:
- "あなたのLoadBalancerのIPレンジ"</code></pre>
</div>
</body>
</html>