How to get client IP in server application
Today, I will talk to you about how to get client-side IP in server-side applications. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
If there is a request header for x-forwarded-for, take the first IP, otherwise take the remoteAddr to establish a connection to the socket.
X-forwarded-for has basically become a standard HTTP header based on proxy. The format is as follows. You can see that the first IP represents its real IP. Please refer to MDN X-Forwarded-For [1].
X-Forwarded-For: 203.0.113.195, 70.41.3.18, 150.172.238.178
X-Forwarded-For:,
Here is how koa gets the IP
Get ips () {
Const proxy = this.app.proxy
Const val = this.get (this.app.proxyIpHeader)
Let ips = proxy & & val
? Val.split (/\ sstories,\ slots /)
: []
If (this.app.maxIpsCount > 0) {
Ips = ips.slice (- this.app.maxIpsCount)
}
Return ips
}
Get ip () {
If (! this [IP]) {
This [IP] = this.ips [0] | | this.socket.remoteAddress | |''
}
Return this [IP]
}
After reading the above, do you have any further understanding of how to obtain client-side IP in server-side applications? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.