Via Nginx reverse proxy, Tomcat gets the real client IP rather than the server IP nginx tomcat slow Nginx proxy Tomcat 40

Source: Internet
Author: User
Tags nginx reverse proxy
Through Nginx reverse proxy, will not get the real IP, is to obtain the Nginx IP, to get the real IP to be configured nginx configuration file: nginx.conf

Proxy_set_header X-real-ip $remote _addr;

For example:

######################################################################## #要转发地域名: Upstream t.csdn.com {        server 192.168.1.188:8080 max_fails=0 weight=1; #8080为tomcat端口    }##################################################################        server {        Listen ;        server_name t.csdn.com;        Access_log/data/wwwlogs/access_tomcat.log combined;        Root/usr/local/tomcat/webapps;        Index index.html index.jsp;        #反向代理配置, all requests for http://hostname are forwarded to the target server defined in upstream. Location        /{            #此处配置的域名必须与upstream的域名一致 to forward.            proxy_pass     http://t.csdn.com;            Proxy_set_header   x-real-ip $remote _addr;            }            #启用nginx Status Monitor page        location/nginxstatus {            stub_status on;            Access_log on;        }        }

Then Tomcat gets the way: Java

private static String Getremoteaddrip (HttpServletRequest request) {string Ipfromnginx = GetHeader (Request, "X-real-ip") ; Log.info ("Ipfromnginx:" + Ipfromnginx); Log.info ("getremoteaddr:" + request.getremoteaddr ()); return Stringutils.isempty (Ipfromnginx)? REQUEST.GETREMOTEADDR (): Ipfromnginx;} private static string GetHeader (HttpServletRequest request, String headname) {String value = Request.getheader (headname) ; return (Stringutils.isnotblank (value) &&! " Unknown ". Equalsignorecase (value))? Value: "";}
Finally, call Getremoteaddrip this method to get the IP:

String ClientIP = Getremoteaddrip (Request), Log.info ("Client IP:" + clientip);


The above describes the Nginx reverse proxy, Tomcat to obtain a real client IP instead of the server IP, including the Tomcat,nginx aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.