2016-12-10 93 views
0

問題是PHP字段$_SERVER['HTTP_CLIENT_IP']未在我的以下站點結構中設置,我應如何更正此問題?如何阻止客戶端IP地址在Cloudflare/Docker/NGINX/LAMP站點中迷路?

命名爲 '回' 泊塢窗橋接網絡(後面的CloudFlare的主機上)上靜態IP容器:

  1. NGINX在容器中運行(172.24.02)
  2. 的Apache + PHP的容器( 172.24.0.3)

nginx.conf:

location/{ 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_set_header X-NginX-Proxy true; 
    proxy_pass http://172.24.0.3; 
} 

test.php的

<?php 

echo('HTTP_CLIENT_IP: '.$_SERVER['HTTP_CLIENT_IP']); 
echo('<br>HTTP_X_FORWARDE‌​D_FOR'.$_SERVER['HTTP_X_FORWARDE‌​D_FOR']); 
echo('<br>REMOTE_ADDR'.$_SERVER['REMOTE_ADDR']); 
echo('<br>HTTP_X_FORWARDED'.$_SERVER['HTTP_X_FORWARDED']); 
echo('<br>HTTP_FORWARDED'.$_SERVER['HTTP_FORWARDED']); 
echo('<br>HTTP_FORWARDED_FOR'.$_SERVER['HTTP_FORWARDED_FOR']); 

?> 

運行上面的產量:

HTTP_CLIENT_IP: 
HTTP_X_FORWARDE‌​D_FOR: 
REMOTE_ADDR: 172.24.0.2 
HTTP_X_FORWARDED: 
HTTP_FORWARDED: 
HTTP_FORWARDED_FOR: 
+0

你是如何配置Nginx和Apache一起工作的? – Devon

+2

HTTP_CLIENT_IP不是標準頭,REMOTE_ADDR是IP的標準,但在代理情況下,您需要設置標頭。爲什麼不打印出所有的頭文件'print_r($ _ SERVER)',而不是打印出與所定義的頭文件不一致的文件。 – Devon

+0

@Devon Apache在端口80上運行在一個容器中,nginx只需將請求轉發給它,並使用上述配置傳遞標頭。我將嘗試您的建議重新打印出整個變量 – James

回答

1

感謝@Devon製作打印出$ _SERVER變量的建議,原來以下字段有客戶端IP:

$_SERVER['HTTP_CF_CONNECTING_IP'] 
$_SERVER['HTTP_X_FORWARDED_FOR'] 

第一個給第二個給出了client IP, cloudflare IP