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

View File

@@ -163,6 +163,11 @@ kubectl logs -l app.kubernetes.io/name=phpfpm -c nginx
|-----------|------|-----------|
| `nginx.customConfig.enabled` | カスタム設定有効化 | `false` |
| `nginx.customConfig.snippet` | 設定スニペット | `""` |
| `nginx.forwardRealIP.enabled` | リアルIP取得有効化 | `false` |
| `nginx.forwardRealIP.header` | リアルIP取得ヘッダー | `"X-Forwarded-For"` |
| `nginx.forwardRealIP.recursive` | 再帰的IP検索 | `true` |
| `nginx.forwardRealIP.trustedProxies` | 信頼するプロキシネットワーク | `["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]` |
| `nginx.forwardRealIP.additionalTrustedProxies` | 追加の信頼プロキシ | `[]` |
## 使用例
@@ -347,7 +352,62 @@ persistence:
enabled: true
```
### 例7: 画像処理アプリケーションGD拡張
### 例7: ベアメタル環境でのリアルIP取得
ベアメタルKubernetesやLoadBalancer経由でのアクセスで、PHPからクライアントの実IPアドレスを取得する設定です。
```yaml
# values.yaml
service:
type: LoadBalancer
nginx:
forwardRealIP:
# リアルIP取得機能を有効化
enabled: true
# リアルIPを取得するヘッダー環境に応じて変更
# - X-Real-IP: シンプルな1段プロキシ
# - X-Forwarded-For: 多段プロキシ対応(推奨)
header: "X-Forwarded-For"
# 再帰的にリアルIPを検索多段プロキシ環境で推奨
recursive: true
# 信頼するプロキシのネットワーク範囲
# ベアメタルクラスターのPodネットワークとServiceネットワークを指定
trustedProxies:
- "10.0.0.0/8" # Kubernetesデフォルトのクラスタネットワーク
- "172.16.0.0/12" # Dockerデフォルトネットワーク
- "192.168.0.0/16" # プライベートネットワーク
# 追加で信頼するプロキシ外部LoadBalancerなど
additionalTrustedProxies:
- "203.0.113.0/24" # 外部LoadBalancerのIPレンジ
persistence:
enabled: true
```
この設定により、PHP側で以下の変数から実IPアドレスが取得できます
- `$_SERVER['REMOTE_ADDR']` - クライアントの実IPアドレス
- `$_SERVER['HTTP_X_REAL_IP']` - 同上(互換性用)
- `$_SERVER['HTTP_X_FORWARDED_FOR']` - プロキシチェーン全体
**検証用PHPコード:**
```php
<?php
echo "Real IP: " . $_SERVER['REMOTE_ADDR'] . "\n";
echo "X-Real-IP: " . ($_SERVER['HTTP_X_REAL_IP'] ?? 'not set') . "\n";
echo "X-Forwarded-For: " . ($_SERVER['HTTP_X_FORWARDED_FOR'] ?? 'not set') . "\n";
```
**注意事項:**
- `trustedProxies`には、信頼できるプロキシLoadBalancer、Ingressコントローラーのみを指定してください
- 不正なプロキシを信頼すると、IPスプーフィング攻撃のリスクがあります
- `customConfig.snippet`との併用も可能です(競合しません)
### 例8: 画像処理アプリケーションGD拡張
```yaml
# values.yaml
@@ -378,7 +438,7 @@ ingress:
- host: images.example.com
```
### 例8: 本番環境構成(フル機能)
### 例9: 本番環境構成(フル機能)
```yaml
# production-values.yaml