xiguaqq20 发表于 2015-11-20 12:22:34

Tomcat通过Haproxy获取访问的源IP

  1.Haproxy端配置
  # more haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
      log 127.0.0.1   local3
      log 127.0.0.1   local1 notice
      maxconn 1000
      pidfile /data/haproxy/logs/haproxy.pid
      daemon
      nbproc 2

  defaults
      log global
      mode http
      option httplog
      option dontlognull
      retries 3
      maxconn 2000
      contimeout 600000
      clitimeout 600000
      srvtimeout 600000
      option redispatch
      option forwardfor

  2.Tomcat端配置
  # more server.xml
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the &quot;License&quot;); you may not use this file except in compliance with
the License.You may obtain a copy of the License at

  <Valve className=&quot;org.apache.catalina.valves.AccessLogValve&quot; directory=&quot;
logs&quot; prefix=&quot;localhost_access_log.&quot; suffix=&quot;.txt&quot; pattern='%h %l %T %t &quot;%r&quot; %s
%b &quot;%{Referer}i&quot; &quot;%{User-Agent}i&quot; %{X-Forwarded-For}i'resolveHosts=&quot;false&quot; />

  2.验证效果

  192.168.1.20 - 0.002 &quot;GET /asf-logo.png HTTP/1.1&quot; 304 - &quot;http://192.168.1.20/&quot; &quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729;.NET CLR 3.0.30729; Media Center PC 6.0; SE 2.X MetaSr 1.0)&quot; 192.168.1.9
192.168.1.20 - 0.005 &quot;GET /bg-middle.png HTTP/1.1&quot; 304 - &quot;http://192.168.1.20/&quot; &quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media CenterPC 6.0; SE 2.X MetaSr 1.0)&quot; -
192.168.1.20 - 0.011 &quot;GET /bg-button.png HTTP/1.1&quot; 304 - &quot;http://192.168.1.20/&quot; &quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media CenterPC 6.0; SE 2.X MetaSr 1.0)&quot; 192.168.1.9

  
页: [1]
查看完整版本: Tomcat通过Haproxy获取访问的源IP