差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

兩邊的前次修訂版 前次修改
下次修改
前次修改
tech:http_realip [2025/10/03 19:50] jonathantech:http_realip [2025/10/03 22:57] (目前版本) – [以下透過一個實驗環境(Cloudflare Tunnel、NPM 與 Docker Swarm 架構)的架構案例進行說明] jonathan
行 1: 行 1:
 ====== 網站取得用戶真實 IP 的方法 ====== ====== 網站取得用戶真實 IP 的方法 ======
-  * 目前網站的網路架構愈來愈複雜, 要取得用戶真實的 IP 需要透過特別指定與層層的傳遞才有機會取得以下透過一個實驗環境的架構案例進行說明+  * 目前網站的網路架構愈來愈複雜, 要取得用戶真實的 IP 需要透過特別指定與層層的傳遞才有機會取得 
 + 
 +==== 通用原則:掌握 HTTP Real IP 標頭 ==== 
 +  * 取得真實 IP 的核心在於: 
 +    - 識別上游代理伺服器傳來的 真實 IP 標頭(例如:**X-Forwarded-For、CF-Connecting-IP、X-Real-IP**)。 
 +    - 使用 **set_real_ip_from** 信任該標頭的來源 IP 範圍。 
 +    - 使用 **real_ip_header** 指定要讀取的標頭名稱。  
 + 
 +==== 以下透過一個實驗環境(Cloudflare Tunnel、NPM 與 Docker Swarm 的架構)案例進行說明 ====
  
 <mermaid> <mermaid>
行 7: 行 15:
     A[Client - 1.2.3.4]     A[Client - 1.2.3.4]
     B{Cloudflare CDN}     B{Cloudflare CDN}
-    C[Host A - Cloudflared]+    C[Host A - 192.168.1.254 - Cloudflared]
     D[Host B - 192.168.1.100:80 - NPM]     D[Host B - 192.168.1.100:80 - NPM]
-    E[Backend Service - dokuwiki_dokuwiki:80] +    E[Docker Swarm Overlay Network - rproxy-net
-    R([Response]) +    F[Backend Service - dokuwiki_dokuwiki:80]
-    F[Docker Swarm Overlay Network - rproxy-net]+
          
     %% 2. 結構化 - 邏輯分區     %% 2. 結構化 - 邏輯分區
行 21: 行 28:
     subgraph Host B Docker Swarm & NPM     subgraph Host B Docker Swarm & NPM
         %% Host A 到 Host B 的連線         %% Host A 到 Host B 的連線
-        C -- HTTP - LAN 192.168.1.x --> D+        C -- HTTP - LAN 192.168.1.x (X-Forwarded-For 開始累積,CF-Connecting-IP 帶有真實 IP) --> D
         %% NPM 經由 Overlay Network 到後端         %% NPM 經由 Overlay Network 到後端
-        D -- HTTP --> E+        D -- HTTP - rproxy-net 10.0.8.x --> E
     end     end
          
     subgraph Backend Service     subgraph Backend Service
-        E -- dokuwiki-net 接入 --> F+        E -- rproxy-net 接入 --> F
     end     end
- 
- 
-    %% 3. 主要流程連線 
-    A --> B 
-    B --> C 
-    C --> D 
-    D --> E 
-    E --> R 
          
     %% 4. 補充細節/網絡連線     %% 4. 補充細節/網絡連線
-    D -- 連線使用 rproxy-net --> F 
     F -- 服務名稱解析 --> E     F -- 服務名稱解析 --> E
 </mermaid> </mermaid>
  
-如果沒有特別設定, 因為 Docker Swarm 預設 Port Mode:Ingress 所以在 Backend Service 看到的用戶 IP 就會是 docker networks 的 ingress overlay 的 IP 網段 Exp. 10.0.0.0/24+如果沒有特別設定, 因為 Docker Swarm 預設 Port Mode:Ingress 所以在 Backend Service 看到的用戶 IP 就會是 docker networks 的 rproxy-net overlay 的 IP 網段 Exp. 10.0.8.0/24
  
 ===== 1. NPM 必須要將 Port Mode 改成 Host ====== ===== 1. NPM 必須要將 Port Mode 改成 Host ======
行 115: 行 113:
 : :
 </cli> </cli>
 +<note> 
 +當設定 real_ip_recursive on 時,Nginx 會從 X-Forwarded-For 列表的最右邊開始,依序移除屬於 set_real_ip_from 信任範圍內的 IP,直到遇到第一個非信任 IP。由於我們這裡直接使用 real_ip_header CF-Connecting-IP,因此它會被視為真實 IP 並覆蓋 $remote_addr。」 
 +</note>
 ===== 3. Backend Service 內的 Nginx Config 設定 ===== ===== 3. Backend Service 內的 Nginx Config 設定 =====
   * 因為這 Backend Service 是 dokuwiki 使用的 image 為 lscr.io/linuxserver/dokuwiki:latest 所以可以透過 custom-cont-init.d 自訂 script 來達成   * 因為這 Backend Service 是 dokuwiki 使用的 image 為 lscr.io/linuxserver/dokuwiki:latest 所以可以透過 custom-cont-init.d 自訂 script 來達成
行 135: 行 135:
     # Real IP 設定 - 信任 Docker Overlay 網路 (NPM IP)     # Real IP 設定 - 信任 Docker Overlay 網路 (NPM IP)
     #     #
-    set_real_ip_from 10.0.0.0/8;+    set_real_ip_from 10.0.8.0/24;
  
     # 讀取 NPM 傳來的 X-Real-IP 標頭     # 讀取 NPM 傳來的 X-Real-IP 標頭
行 144: 行 144:
  
 # 檢查配置是否已經存在 # 檢查配置是否已經存在
-if grep -q "set_real_ip_from 10.0.0.0/8;" "$SITE_CONF"; then+if grep -q "set_real_ip_from 10.0.8.0/24;" "$SITE_CONF"; then
     echo ">> [custom-init] Real IP configuration already exists in site-conf. Skipping."     echo ">> [custom-init] Real IP configuration already exists in site-conf. Skipping."
 else else
行 165: 行 165:
 chmod +x "$0" chmod +x "$0"
 </file> </file>
 +  * 需要將這個 script 檔案設定執行權限 <cli>chmod a+x 10-nginx-realip.sh</cli>
 +  * 重新啟動 dokuwiki 這個 stack 
 +  * 查看 dokuwiki 啟動的容器 log 應該就會出現類似以下的訊息 <file>
 +:
 +**** Permissions could not be set. This is probably because your volume mounts are remote or read-only. ****
 +**** The app may not work properly and we will not provide support for it. ****
 +Existing install found, deleting install.php.
 +[custom-init] Files found, executing
 +[custom-init] 10-nginx-realip.sh: executing...
 +>> [custom-init] Configuring Nginx Real IP in /config/nginx/site-confs/default.conf...
 +>> [custom-init] Real IP configuration already exists in site-conf. Skipping.
 +[custom-init] 10-nginx-realip.sh: exited 0
 +[ls.io-init] done.
 +:
 +</file>
 +  * 接下來就可以查看 dokuwiki 的 nginx access log 可以出現用戶 IP Exp. <cli>
 +:
 +15.235.145.214 - - [03/Oct/2025:19:55:15 +0800] "GET /tech/dokuwiki_plugin/poll?do=login&sectok= HTTP/1.1" 200 6278 "https://www.ichiayi.com/tech/dokuwiki_plugin/poll" "Mozilla/5.0 (Macintosh; Intel Mac OS X 12.5; rv:114.0) Gecko/20100101 Firefox/114.0"
 +15.235.145.214 - - [03/Oct/2025:19:55:15 +0800] "GET /tech/dokuwiki_plugin/poll?do=register HTTP/1.1" 200 8469 "https://www.ichiayi.com/tech/dokuwiki_plugin/poll?do=login&sectok=" "Mozilla/5.0 (Macintosh; Intel Mac OS X 12.5; rv:114.0) Gecko/20100101 Firefox/114.0"
 +15.235.145.214 - - [03/Oct/2025:19:55:16 +0800] "GET /tech/dokuwiki_plugin/poll?q=&do=search HTTP/1.1" 200 8417 "https://www.ichiayi.com/tech/dokuwiki_plugin/poll?do=register" "Mozilla/5.0 (Macintosh; Intel Mac OS X 12.5; rv:114.0) Gecko/20100101 Firefox/114.0"
 +217.113.196.92 - - [03/Oct/2025:19:55:26 +0800] "GET /git?idx=tech%3Alvm HTTP/1.1" 200 21886 "-" "Mozilla/5.0 (compatible; IbouBot/1.0; [email protected]; +https://ibou.io/iboubot.html)"
 +82.97.199.97 - - [03/Oct/2025:19:55:34 +0800] "GET /tech/pvetips?difftype=inline&do=diff&rev2%5B0%5D=1548307963&rev2%5B1%5D=1597387945 HTTP/1.1" 200 8896 "-" "Brightbot 1.0"
 +:
 +</cli>
  
 +{{tag>tips docker swarm network}}
  • tech/http_realip.1759492258.txt.gz
  • 上一次變更: 2025/10/03 19:50
  • jonathan