Why to get client IP:
PHP: We need to get the IP information of the user client to identify the user location, but now we get the address is always Hangzhou front-end: I check, a little wait .... Understand, we added a layer node server, the server in Hangzhou, you get the node server ipphp: What to do? Front end: I'll give you an IP field. php: Oh, the front end: (crackling ...) Okay, do you have a look at the value? PHP: Yes, but the value is not right, you pass to me is :: FFFF:127.0. 0.1
Analysis Reason:
The IP field value of the front-end to PHP is req.connection.remoteAddress , theoretically nothing wrong, but actually get is IPv6 address, PHP need IPv4 address.
Nodejs website API:
NET Module Lookup Server.listen ()
Server.listen ([port][, host][, backlog][, callback])
Start a TCP service to listen for the input port and host.
If host is omitted, if IPv6 is available, the server will receive a link based on unspecified IPv6 address (::), otherwise receive unspecified IPV4 address (0.0.0.0)
Modification Method:
Specifies that the host parameter is 0.0.0.0, which is listen (port, ' 0.0.0.0 ', () = {});
For example:
Final Result:
Front End: Yes, you can look at PHP: I'll try ~ wait ... ok,bingo.
Summarizes a method of acquiring IP:
/** *function getclientip (req ) {return// Determine if there is a reverse proxy IP // determine the IP of the connection remote IP // judgment backend socket req.connection.socket.remoteAddress;};
Nodejs getting the client IP address