In nginx reverse proxy, thinkphp and php cannot obtain the correct Internet ip address. nginxthinkphp

Source: Internet
Author: User
Tags nginx reverse proxy

In nginx reverse proxy, thinkphp and php cannot obtain the correct Internet ip address. nginxthinkphp

When a user needs to obtain the user's ip address to send a text message, tp always obtains the Intranet ip Address: 10.10.10.10.

Tp framework ip Retrieval Method: get_client_ip

1/** 2 * Get Client IP address 3 * @ param integer $ type return type 0 return IP address 1 return IPV4 address number 4 * @ param boolean $ adv whether to perform advanced mode acquisition (may be disguised) 5 * @ return mixed 6 */7 function get_client_ip ($ type = 0, $ adv = false) {8 $ type = $ type? 1: 0; 9 static $ ip = NULL; 10 if ($ ip! = NULL) return $ ip [$ type]; 11 if ($ adv) {12 if (isset ($ _ SERVER ['HTTP _ X_FORWARDED_FOR ']) {13 $ arr = explode (',', $ _ SERVER ['HTTP _ X_FORWARDED_FOR ']); 14 $ pos = array_search ('unknown', $ arr ); 15 if (false! ==$ Pos) unset ($ arr [$ pos]); 16 $ ip = trim ($ arr [0]); 17} elseif (isset ($ _ SERVER ['HTTP _ CLIENT_IP ']) {18 $ ip = $ _ SERVER ['HTTP _ CLIENT_IP']; 19} elseif (isset ($ _ SERVER ['remote _ ADDR ']) {20 $ ip = $ _ SERVER ['remote _ ADDR']; 21} 22} elseif (isset ($ _ SERVER ['remote _ ADDR ']) {23 $ ip = $ _ SERVER ['remote _ ADDR']; 24} 25 // valid ip address verification 26 $ long = sprintf ("% u", ip2long ($ ip); 27 $ ip = $ long? Array ($ ip, $ long): array ('0. 0.0.0 ', 0); 28 return $ ip [$ type]; 29}View Code

For some reason, the w project is under Apache, and some other projects are crowded in to use nginx. The nginx project requires port 80 authorization, so reverse proxy is used.

After reverse proxy, the web server cannot directly obtain the ip address of the client because the intermediate layer is added between the client and the web server. the ip address of the reverse proxy server is obtained through the $ remote_addr variable.
The built-in function of the tp framework obtains $ remote_addr, so we cannot obtain the real ip address.

Open/usr/local/reverse_proxy_nginx/conf/nginx. conf and you can see the following key configurations:

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 Internet ip address, so you only need to get HTTP_X_FORWARDED_FOR or HTTP_X_REAL_IP.
The modified tp framework functions are as follows:
ThinkPHP \ Common \ functions. php
1/* 2 * access using localhost. The read result is ": 1", which is normal. 3 *: 1 indicates that ipv6 support is enabled, which indicates the local loopback address under ipv6. 4 * access through an IP address or disable ipv6 support is not displayed. 5 */6 function get_client_ip () {7 $ ip = "unknown"; 8 if (isset ($ _ SERVER )) {9 if (isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]) {10 $ ip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"]; 11} elseif (isset ($ _ SERVER ["HTTP_CLIENT_ip"]) {12 $ ip = $ _ SERVER ["HTTP_CLIENT_ip"]; 13} else {14 $ ip = $ _ SERVER ["REMOTE_ADDR"]; 15} 16} else {17 if (getenv ('HTTP _ X_FORWARDED_FOR ')) {18 $ ip = getenv ('HTTP _ X_FORWARDED_FOR '); 19} elseif (getenv ('HTTP _ CLIENT_ip ')) {20 $ ip = getenv ('HTTP _ CLIENT_ip '); 21} else {22 $ ip = getenv ('remote _ ADDR '); 23} 24} 25 if (trim ($ ip) = ": 1") {26 $ ip = "127.0.0.1"; 27} 28 return $ ip; 29}View Code

For the tp project under Apache, call the get_client_ip function to get the Internet ip address.



 

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.