ProxyPass, ProxyReverse vs AJP

对着背影说爱祢 提交于 2019-12-02 20:46:48

Do it like this:

in the apache config:

<Location /foo>
  ProxyPass ajp://localhost:8009/foo
  ProxyPassReverse ajp://localhost:8009/foo
</Location>

And then in your server.xml:

<Connector port="8009" 
           enableLookups="false" secure="true" URIEncoding="UTF-8"
           tomcatAuthentication="false"
           protocol="AJP/1.3" />

That should pass everything through. The AJP protocol passes the info, but http: doesn't.

You may not want secure="true", I use that because SSL is handled at the apache layer and I need tomcat to know that the connection should be considered a secure one.

You can read the X-Forwarded-For in the request header.

From the Apache mod_proxy documentation:

When acting in a reverse-proxy mode (using the ProxyPass directive, for example), mod_proxy_http adds several request headers in order to pass information to the origin server. These headers are:

  • X-Forwarded-For: The IP address of the client.
  • X-Forwarded-Host: The original host requested by the client in the Host HTTP request header.
  • X-Forwarded-Server: The hostname of the proxy server.

Be careful when using these headers on the origin server, since they will contain more than one (comma-separated) value if the original request already contained one of these headers. For example, you can use %{X-Forwarded-For}i in the log format string of the origin server to log the original clients IP address, but you may get more than one address if the request passes through several proxies.

In your servlet, you would have:

doGet(HttpServletRequest request, HttpServletResponse response){
  request.getHeader("X-Forwarded-For")
}

this is very simple:

<VirtualHost> 

 ServerName www.server.com

 redirect / http://www.server.com/foo

 ProxyRequests off
 ProxyPass / ajp://localhost:8009/

</VirtualHost>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!