Configuring Tomcat's access log format output

Source: Internet
Author: User
Tags nginx server

This document describes how to configure the access log for Tomcat and output the specified log format as required. and in the Nginx+tomcat configuration environment, how to let Tomcat record the guest real IP address。 This article is original content, reprint please indicate source: Jdiy official website Http://jdiy.net/read.jsp?id=y0hab3qs03 in Tomcat's Server.xml file, the host configuration area is found similar to the following (red section) That is, the configuration for the Access log: Unpackwars= "true" autodeploy= "true" > <!--... Part of the content slightly. -
<valve classname= "Org.apache.catalina.valves.AccessLogValve" directory= "Logs"
prefix= "Localhost_access_log" suffix= ". txt"
pattern= "%h%l%u%t &quot;%r&quot; %s%b "/>
</Host> the directory used to specify the log storage path, by default in the Tomcat logs directory, for example, we can modify to: directory= "C:/wwwlogs" to put the log into the C:\wwwlogs directory. The prefix and suffic, respectively, are used to specify the prefix and suffix of the log file, without me having to say more. Now let's take a look at the Pattern configuration section, which specifies the output format for the log. A valid log format pattern can be found in the following string, where the corresponding information is replaced by the specified response content:

%a-Remote IP address
%a-Local IP address
%b-The number of bytes sent, excluding HTTP headers, or "-" if no bytes were sent
%b-Number of bytes sent, not including HTTP headers
%H-Remote Host name
%H-Request Agreement
%l (lowercase L)-remote logic from IDENTD username (always return '-')
%m-Request Method
%p-Local Port
%q-Query string (preceded by a "? "If it exists, it is an empty string
%r-Requirements for the first line
%s-HTTP status code for the response
%s-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)-the thread name of the current request

Additionally, you can specify the following aliases to be set as one of the universally used modes:
Common-%h%l%u%t "%r"%s%b
Combined-%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 represents incoming headers (HTTP Request)%{xxx}o xxx represents outgoing?? The response header (Http resonse)
%{xxx}c XXX represents a specific cookie name
%{xxx}r XXX stands for servletrequest property name%{xxx}s xxx for attribute name in HttpSession To illustrate:For example, when we set up a JSP server, we use Nginx+tomcatIn this configuration, the request is forwarded to Tomcat by the Nginx, and when it is necessary to record the caller's real IP address information in the Tomcat log, we need to do a little bit different from the other special matches, otherwise the Tomcat recorded guest IP is all 127.0.0.1, This is because all requests are forwarded by the Nginx front-end server, and the front-end server is 127.0.0.1 for Tomcat. Below, let's look at how to get Tomcat to record the user's real IP address: First, configure the Nginx forwarding IP header:In the Nginx Server Host configuration section, add: Proxy_set_header host $host;
Proxy_set_header x-real-ip $remote _addr; Description: The above two lines are used to send a real remote host name and IP address to Tomcat. Where host represents the hostname, X-REAL-IP represents the host IP, and the variables are case-insensitive for HTTP header content. Second, configure the Tomcat logging customer real IP:To record the visitor's real IP in Tomcat, refer to the Tomcat log configuration syntax described above, just add the following pattern to the log mode:%{x-real-ip}i The following full tomcat log configuration segment: <valve classname= " Org.apache.catalina.valves.AccessLogValve " directory=" c:/wwwlogs/"prefix=" cluster. "suffix=". Log "pattern=" %{x-real-ip}i %u%t%r%s%b "resolvehosts=" false "/>  Note: After 22 modifications, you should restart the Nginx and Tomcat services for your changes to take effect. This way, when a new request comes in, the real IP address of the guest can be recorded in the Tomcat log file.  ===================================== above for reprint native configuration (without nginx): Server.xml: <valve classname= " Org.apache.catalina.valves.AccessLogValve "directory=" logs "                prefix= "Localhost_access_log" suffix= ". txt"                 pattern = "%h%l%u%t%r%s%b%d%{cookie}i%{user-agent}i%{a}r"                 res Olvehosts= "false"/> Output: 127.0.0.1--[25/feb/2013:11:57:29 +0800] get/app_back/index.do?a=12345 http/1.1 200 59847 688 jsessionid=5f171789ddf29ab377f68b52bb18884e; MysesSionid=3af66463-79a3-4b6c-a4c5-e7cd39a0aead mozilla/5.0 (Windows NT 5.1; rv:16.0) gecko/20100101 Firefox/16.0-where The parameter A in the URL can be displayed in the URL, but the parameters in the request submitted by post are not recorded.

Configure the Access log formatted output of Tomcat

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.