Tomcat program logs client real IP

Source: Internet
Author: User
Tags nginx server

Demand:

Development notification: Let the backend tomcat log get the real IP, not the nginx server's IP

Tomcat is in front of the nginx-made reverse proxy, so Tomcat takes the Nginx IP.

Journal name is Localhost_access_log.txt

In fact, to solve this problem, only need to modify two places:

1. Modify the Nginx configuration file

Add in Server or location

Proxy_set_header x-forwarded-for $remote _addr;

2. Modifying the Tomcat configuration file

Modify the Server.xml file in the file

Modify the contents of the pattern parameter in the classname= "Org.apache.catalina.valves.AccessLogValve" directory= "logs" block to the above red font content, i.e.:

pattern="%{x-forwarded-for}i%l%u%t%r%s%b%d%q%{user-agent}i" resolvehosts= "false"

Complete configuration after the modification:

 <valve classname=  Org.apache.catalina.valves.AccessLogValve   Directory= " logs  "   = localhost_access_log suffix="  .txt   "  ="  %{x-forwarded-for}i%l%u%t%r%s%b%d%q%{user-agent}i   resolvehosts=< Span style= "color: #800000;" > " false  " /> 

Detailed Pattern configuration parameters:

The pattern configuration segment that specifies the output format for the log. The log parameters are as follows:

%a-Remote IP address%a-Local IP address%b-The number of bytes sent, not including the HTTP header, or "-"If no bytes are sent%b-number of bytes sent, not including HTTP header%h-remote Host name%h-Request Protocol%l (lowercase L)-Remote logic from the IDENTD user name (always return' - ')%m-Request Method%p-Local Port%q-query string (preceded by a "? "If it exists, it is an empty string%r-requirement for first line%s-HTTP status Code%s for response-User session Id%t-date and time, in common log format%u-remote user authentication%u-URL Path of the request%v-local server name%d-time (in milliseconds) to process the request%t-time (in seconds) to process the request%i (uppercase I)-thread name of the current request Additionally, you can specify the following alias to be set as one of the universally used modes: common-%h%l%u%t"%r"%s%bcombined-%h%l%u%t"%r"%s%b"%{referer}i" "%{user-agent}i" 

In addition, you can write the query parameters of request requests, session variable values, cookie values, or variable values for HTTP request/response header content to a log file.

It is modeled after the Apache syntax:

%{xxx}i XXX on behalf of incoming headers (HTTP Request)

%{xxx}o XXX stands for outgoing response headers (Http resonse)

%{xxx}c XXX represents a specific cookie name

%{xxx}r XXX stands for ServletRequest property name

%{xxx}s XXX represents the attribute name in HttpSession

Pattern parameter Detailed reference: http://sofar.blog.51cto.com/353572/1712069/

Tomcat program logs client real IP

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.