Nginx reverse proxy under thinkphp, PHP not get the correct external network IP

Source: Internet
Author: User
Tags get ip nginx reverse proxy

When recording users to send text messages need to obtain the user ip, TP has been acquired by the intranet ip:10.10.10.10

TP Framework Get IP method:get_client_ip

1 /**2 * Get client IP address3 * @param integer $type return type 0 return IP address 1 return IPV4 address number4 * @param boolean $ADV whether advanced mode fetching (possibly Disguised)5 * @return Mixed6  */7 functionGET_CLIENT_IP ($type= 0,$adv=false) {8     $type=$type? 1:0;9     Static $ip=NULL;Ten     if($ip!==NULL)return $ip[$type]; one     if($adv){ a         if(isset($_server[' http_x_forwarded_for '])) { -             $arr=Explode(‘,‘,$_server[' http_x_forwarded_for ']); -             $pos=Array_search(' Unknown ',$arr); the             if(false!==$pos)unset($arr[$pos]); -             $ip=Trim($arr[0]); -}ElseIf(isset($_server[' Http_client_ip '])) { -             $ip=$_server[' Http_client_ip ']; +}ElseIf(isset($_server[' REMOTE_ADDR '])) { -             $ip=$_server[' REMOTE_ADDR ']; +         } a}ElseIf(isset($_server[' REMOTE_ADDR '])) { at         $ip=$_server[' REMOTE_ADDR ']; -     } -     //IP Address legal authentication -     $long=sprintf("%u",Ip2long($ip)); -     $ip=$long?Array($ip,$long) :Array(' 0.0.0.0 ', 0); -     return $ip[$type]; in}
View Code

For some reason, the W project was under apache, and then some other projects were squeezed in using the project under Nginx,nginx requires also 80 port authorization, so the reverse proxy is Used.

After the reverse proxy, due to the addition of the middle tier between the client and the Web server, so the Web server can not directly get the Client's ip, through the $remote _addr variable to get the reverse proxy server IP address
The TP frame comes with a function to get the $remote _addr, so we can't get the real ip.

Open /usr/local/reverse_proxy_nginx/conf/nginx.conf to see the following key configuration

location/ {            proxy_pass http://backend2;            # Proxy Settings             proxy_redirect     off;            Proxy_set_header   Host             $host;            Proxy_set_header   X-real-ip        $remote _addr;            Proxy_set_header   X-forwarded-for  $proxy _add_x_forwarded_for;}

Test

        $remoteaddr=$_server["remote_addr"]; Echo $remoteaddr; Echo"---"; $remotehost=$_server["remote_host"]; Echo $remotehost; Echo"---"; $xforwardedfor=$_server["http_x_forwarded_for"]; Echo $xforwardedfor; Echo"---"; $xrealip=$_server["http_x_real_ip"]; Echo $xrealip;

Output

10.10.10.123------181.128.136.191---181.128.136.191
181.128.136.191 is your real external network IP, so you just need to get http_x_forwarded_for or http_x_real_ip .
The following is the modified TP frame function
thinkphp\common\functions.php
1 /*2 * Accessed with localhost, read ":: 1" is the normal Situation. 3 *:: 1 description turned on IPv6 support, which is the representation of the local loopback address under ipv6. 4 * Use IP address to access or close IPv6 support can not display This. 5 */6 functionget_client_ip () {7     $ip= "unknown";8     if(isset($_server)) {9         if(isset($_server["http_x_forwarded_for"])) {Ten             $ip=$_server["http_x_forwarded_for"]; one}ElseIf(isset($_server["http_client_ip"])) { a             $ip=$_server["http_client_ip"]; -}Else { -             $ip=$_server["remote_addr"]; the         } -}Else { -         if(getenv(' http_x_forwarded_for ')) { -             $ip=getenv(' http_x_forwarded_for '); +}ElseIf(getenv(' Http_client_ip ')) { -             $ip=getenv(' Http_client_ip '); +}Else { a             $ip=getenv(' REMOTE_ADDR '); at         } -     } -     if(Trim($ip) = = ":: 1"){ -         $ip= "127.0.0.1"; -     } -     return $ip; in}
View Code

Apache under the TP project, and then call the get_client_ip function will be able to obtain an external network IP



Nginx reverse proxy under thinkphp, PHP not get the correct external network IP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.