Featured image of post 使用 mod_remoteip 模組設定 Apache 取得真實 IP 地址

使用 mod_remoteip 模組設定 Apache 取得真實 IP 地址

詳細教學如何使用 mod_remoteip 模組設定 Apache 取得真實 IP 地址,適用於 Cloudflare、其他 CDN 服務或伺服器處於 Proxy 後面的情況。包含完整配置步驟、故障排除和驗證方法。

最後更新
約 1098 字

前言

當您的網站使用 CDN 服務(如 Cloudflare)或伺服器處於 Proxy 後面時,Apache 的存取日誌中通常只會記錄到 CDN 或 Proxy 的 IP 地址,而不是訪客的真實 IP 地址。這會影響:

  • 網站統計分析
  • 安全監控
  • 地理位置分析
  • 存取控制

透過啟用 Apache 的 mod_remoteip 模組,我們可以輕鬆解決這個問題,讓日誌記錄顯示訪客的真實 IP 地址。

什麼是 mod_remoteip?

mod_remoteip 是 Apache 的一個模組,它能夠:

  • 從 HTTP 標頭中提取真實的客戶端 IP 地址
  • 替換 REMOTE_ADDR 環境變數
  • 支援信任的代理伺服器列表
  • 提供 IP 地址驗證功能

安裝與啟用 mod_remoteip 模組

1. 檢查模組是否已安裝

首先檢查 mod_remoteip 模組是否已經安裝:

1
apache2ctl -M | grep remoteip

如果沒有輸出,表示模組尚未啟用。

2. 啟用 mod_remoteip 模組

1
sudo a2enmod remoteip

3. 重新載入 Apache 配置

1
sudo systemctl reload apache2

配置 mod_remoteip

1. 建立配置檔案

1
sudo nano /etc/apache2/conf-available/remoteip.conf

2. Cloudflare 配置

如果您使用 Cloudflare CDN,添加以下配置:

參考 Cloudflare IP Ranges 的 IP 範圍。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 啟用 mod_remoteip
RemoteIPHeader CF-Connecting-IP

# 設定信任的 Cloudflare IP 範圍
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 104.16.0.0/13
RemoteIPTrustedProxy 104.24.0.0/14
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 131.0.72.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 172.64.0.0/13
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17

# IPv6 支援
RemoteIPTrustedProxy 2400:cb00::/32
RemoteIPTrustedProxy 2606:4700::/32
RemoteIPTrustedProxy 2803:f800::/32
RemoteIPTrustedProxy 2405:b500::/32
RemoteIPTrustedProxy 2405:8100::/32
RemoteIPTrustedProxy 2a06:98c0::/29
RemoteIPTrustedProxy 2c0f:f248::/32

3. 其他 CDN/Proxy 配置

如果您使用其他 CDN 服務或自建 Proxy,使用以下配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 使用 X-Forwarded-For 標頭
RemoteIPHeader X-Forwarded-For

# 設定信任的代理伺服器 IP
RemoteIPTrustedProxy 192.168.1.0/24
RemoteIPTrustedProxy 10.0.0.0/8

# 設定內部 IP 範圍(可選)
RemoteIPInternalProxy 192.168.0.0/16
RemoteIPInternalProxy 10.0.0.0/8
RemoteIPInternalProxy 172.16.0.0/12

4. 啟用配置檔案

1
sudo a2enconf remoteip

日誌格式配置

為了更好地查看真實 IP 地址,建議修改日誌格式:

1. 編輯 Apache 配置

1
sudo nano /etc/apache2/apache2.conf

2. 添加自定義日誌格式

在檔案末尾添加:

1
2
# 自定義日誌格式,顯示真實 IP
LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined_realip

或者可以更改 combined%h%a,這樣會顯示真實的 IP 地址。如果更改了 combined,就不需要另外更改虛擬主機配置。

3. 更新虛擬主機配置

在您的虛擬主機配置中:

1
2
3
4
5
6
7
<VirtualHost *:80>
    # 其他配置...
    
    # 使用自定義日誌格式
    CustomLog ${APACHE_LOG_DIR}/access.log combined_realip
    ErrorLog ${APACHE_LOG_DIR}/error.log
</VirtualHost>

驗證配置

1. 檢查配置語法

1
sudo apache2ctl configtest

如果配置正確,會顯示 Syntax OK

2. 重新啟動 Apache

1
sudo systemctl restart apache2

3. 測試真實 IP 記錄

建立一個簡單的 PHP 測試檔案:

1
sudo nano /var/www/html/test-ip.php

內容:

1
2
3
4
5
<?php
echo "REMOTE_ADDR: " . $_SERVER['REMOTE_ADDR'] . "\n";
echo "HTTP_X_FORWARDED_FOR: " . ($_SERVER['HTTP_X_FORWARDED_FOR'] ?? 'Not set') . "\n";
echo "HTTP_CF_CONNECTING_IP: " . ($_SERVER['HTTP_CF_CONNECTING_IP'] ?? 'Not set') . "\n";
?>

訪問 http://your-domain.com/test-ip.php 查看結果。

故障排除

常見問題

  1. 模組未啟用

    1
    2
    
    sudo a2enmod remoteip
    sudo systemctl restart apache2
    
  2. 配置檔案未啟用

    1
    2
    
    sudo a2enconf remoteip
    sudo systemctl restart apache2
    
  3. IP 範圍不正確

    • 檢查 Cloudflare IP 範圍是否為最新版本
    • 確認代理伺服器 IP 地址正確
  4. 日誌仍顯示代理 IP

    • 確認 RemoteIPHeader 設定正確
    • 檢查 HTTP 標頭是否包含正確的 IP 資訊

檢查日誌

1
2
3
4
5
# 查看 Apache 錯誤日誌
sudo tail -f /var/log/apache2/error.log

# 查看存取日誌
sudo tail -f /var/log/apache2/access.log

安全考量

  1. 信任的代理列表:只添加您信任的代理伺服器 IP 範圍
  2. IP 驗證:定期更新 Cloudflare IP 範圍
  3. 監控:監控日誌以確保配置正常工作

總結

透過正確配置 mod_remoteip 模組,您可以:

  • ✅ 在日誌中記錄訪客的真實 IP 地址
  • ✅ 改善網站統計和分析的準確性
  • ✅ 增強安全監控能力
  • ✅ 支援地理位置分析

記住定期更新信任的代理 IP 範圍,並監控配置是否正常工作。


使用 Hugo 建立
主題 StackJimmy 設計