从负载均衡器后面的服务器获取$ _SERVER ['x_forwarded_for']值

时间:2016-02-04 05:18:31

标签: php load-balancing

基本上我想获得用户IP。但由于我的服务器落后于负载均衡器,因此我从$_SERVER['x_forwarded_for']值获取用户IP。但是,当客户端使用HTTPS连接到服务器时,该值始终缺失。如果客户端使用HTTP连接,这就是我得到的:

Array
(
    [HTTP_HOST] => xxx
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [HTTP_COOKIE] => xxx
    [HTTP_USER_AGENT] => Mozilla/5.0 (iPhone; CPU iPhone OS 9_2_1 like Mac OS X) AppleWebKit/601.1.46$
    [HTTP_ACCEPT_LANGUAGE] => en-gb
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_CONNECTION] => Keep-alive
    [HTTP_VIA] => 1.1 ID-0002262071501332 uproxy-6
    [HTTP_X_FORWARDED_FOR] => 112.215.63.20
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    [SERVER_SIGNATURE] => <address>Apache/2.4.7 (Ubuntu) Server at xxx Port 80</address>

    [SERVER_SOFTWARE] => Apache/2.4.7 (Ubuntu)
    [SERVER_NAME] => xxx
    [SERVER_ADDR] => xxx
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 174.133.117.12
    [DOCUMENT_ROOT] => xxx
    [REQUEST_SCHEME] => http
    [CONTEXT_PREFIX] =>
    [CONTEXT_DOCUMENT_ROOT] => xxx
    [SERVER_ADMIN] => [no address given]
    [SCRIPT_FILENAME] => xxx
    [REMOTE_PORT] => 33268
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] =>
    [REQUEST_URI] => /test.php
    [SCRIPT_NAME] => /test.php
    [PHP_SELF] => /test.php
    [REQUEST_TIME_FLOAT] => 1454562249.848
    [REQUEST_TIME] => 1454562249
)

但是在使用HTTPS时,我得到了这个:

Array
(
    [HTTPS] => on
    [SSL_TLS_SNI] => xxx
    [HTTP_HOST] => xxx
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [HTTP_ACCEPT_LANGUAGE] => en-gb
    [HTTP_CONNECTION] => keep-alive
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_USER_AGENT] => mozilla/5.0 (iphone; cpu iphone os 7_0_2 like mac os x) applewebki$
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    [SERVER_SIGNATURE] => <address>Apache/2.4.7 (Ubuntu) Server at xxx Port 44$

    [SERVER_SOFTWARE] => Apache/2.4.7 (Ubuntu)
    [SERVER_NAME] => xxx
    [SERVER_ADDR] => xxx
    [SERVER_PORT] => 443
    [REMOTE_ADDR] => 174.133.117.12
    [DOCUMENT_ROOT] => xxx
    [REQUEST_SCHEME] => https
    [CONTEXT_PREFIX] =>
    [CONTEXT_DOCUMENT_ROOT] => xxx
    [SERVER_ADMIN] => [no address given]
    [SCRIPT_FILENAME] => xxx
    [REMOTE_PORT] => 57456
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] =>
    [REQUEST_URI] => xxx
    [SCRIPT_NAME] => xxx
    [PHP_SELF] => xxx
    [REQUEST_TIME_FLOAT] => 1454562275.52
    [REQUEST_TIME] => 1454562275
)

真的很感激任何帮助。谢谢!

0 个答案:

没有答案