After nginx reverse proxy, all the IP addresses obtained in the application are the IP addresses of the reverse proxy server, and the obtained domain name is also the Domain Name of the URL configured by the reverse proxy, you need to add some configuration information in the nginx reverse proxy configuration to transfer the real IP address and domain name of the client to the application.
During nginx reverse proxy configuration, the following configuration is generally added:
Proxy_set_header host $ host;
Proxy_set_header X-real-IP $ remote_addr;
Proxy_set_header remote-host $ remote_addr;
Proxy_set_header X-forwarded-for $ proxy_add_x_forwarded_for;
In the first line, the host configuration is about the domain name transfer configuration, and the rest is related to the IP address.
Obtain the real IP address of the client in PHP:
/*** Obtain the Client IP */function getclientip () {$ IP = "unknown";/*** access using localhost, read ": 1 "is normal. *: 1 indicates that IPv6 support is enabled, which indicates the local loopback address under IPv6. * This attribute is not displayed if you use an IP address to access or disable IPv6 support. **/If (isset ($ _ server) {If (isset ($ _ server ["http_x_forwarded_for"]) {$ IP = $ _ server ["http_x_forwarded_for"];} elseif (isset ($ _ server ["http_client_ip"]) {$ IP = $ _ server ["http_client_ip"];} else {$ IP = $ _ server ["remote_addr"];} else {If (getenv ('HTTP _ x_forwarded_for ') {$ IP = getenv ('HTTP _ x_forwarded_for');} elseif (getenv ('HTTP _ client_ip ')) {$ IP = getenv ('HTTP _ client_ip ');} else {$ IP = getenv ('remote _ ADDR');} If (TRIM ($ IP) = ": 1") {$ IP = "127.0.0.1";} return $ IP ;}
Java obtains the real IP address of the client:
public String getClientIP(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; }
PHP retrieves domain names:
PHP code
- $ _ Server ['server _ name'];
Java:
Java code
- Request. getservername ()