Nginx reverse proxy obtains the real IP address and domain name

Source: Internet
Author: User
Tags nginx reverse proxy

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
  1. $ _ Server ['server _ name'];

 

 

Java:

 

Java code
  1. Request. getservername ()

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.