在tomcat的access中打印出請求的狀況能夠幫助咱們分析問題,一般比較關注的有訪問IP、線程號、訪問url、返回狀態碼、訪問時間、持續時間。nginx
在Spring boot中使用了內嵌的tomcat,能夠經過server.tomcat.accesslog
配置tomcat 的access日誌,這裏就以Spring boot 1.5.3爲例。tomcat
server.tomcat.accesslog.buffered=true # Buffer output such that it is only flushed periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for IP address, Hostname, protocol and port used for the request.
server.tomcat.accesslog.rotate=true # Enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
比較經常使用的有(省略了前綴server.tomcat.accesslog.
):服務器
pattern的配置:cookie
enableLookups
for the connector is false),遠程主機名稱(若是resolveHosts爲false則展現IP)Access log 也支持將cookie、header、session或者其餘在ServletRequest中的對象信息打印到日誌中,其配置遵循Apache配置的格式({xxx}指值的名稱):session
%{xxx}i
for incoming headers,request header信息%{xxx}o
for outgoing response headers,response header信息%{xxx}c
for a specific cookie%{xxx}r
xxx is an attribute in the ServletRequest%{xxx}s
xxx is an attribute in the HttpSession%{xxx}t
xxx is an enhanced SimpleDateFormat pattern (see Configuration Reference document for details on supported time patterns)Access log內置了兩個日誌格式模板,能夠直接指定pattern爲模板名稱,如:ide
server.tomcat.accesslog.pattern=common
%h %l %u %t "%r" %s %b
,依次爲:遠程主機名稱,遠程用戶名,被認證的遠程用戶,日期和時間,請求的第一行,response code,發送的字節數%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"
,依次爲:遠程主機名稱,遠程用戶名,被認證的遠程用戶,日期和時間,請求的第一行,response code,發送的字節數,request header的Referer信息,request header的User-Agent信息。除了內置的模板,咱們經常使用的配置有:url
%t %a "%r" %s (%D ms)
,日期和時間,請求來自的IP(不必定是原始IP),請求第一行,response code,響應時間(毫秒),樣例:[21/Mar/2017:00:06:40 +0800] 127.0.0.1 POST /bgc/syncJudgeResult HTTP/1.0 200 63
,這裏請求來自IP就是通過本機的nginx轉發的。%t [%I] %{X-Forwarded-For}i %a %r %s (%D ms)
,日期和時間,線程名,原始IP,請求來自的IP(不必定是原始IP),請求第一行,response code,響應時間(毫秒),樣例:[21/Apr/2017:00:24:40 +0800][http-nio-7001-exec-4] 10.125.15.1 127.0.0.1 POST /bgc/syncJudgeResult HTTP/1.0 200 5
,這裏的第一個IP是Nginx配置了X-Forwarded-For
記錄了原始IP。這裏簡要介紹下上面用到的HTTP請求頭X-Forwarded-For
,它是一個 HTTP 擴展頭部,用來表示 HTTP 請求端真實 IP,其格式爲:X-Forwarded-For: client, proxy1, proxy2
,其中的值經過一個逗號+空格
把多個IP地址區分開,最左邊(client)是最原始客戶端的IP地址,代理服務器每成功收到一個請求,就把請求來源IP地址添加到右邊。spa