Question

I am using httpd with tomcat using the following config:

ProxyPass / ajp://localhost:8009/MyProject
ProxyPassReverse / ajp://localhost:8009/MyProject

This works fine except my image links from tomcat do not work when the HTML renders:

<img src="/MyProject/img/image.jpg"/>

where as I would expect:

  <img src="/img/image.jpg"/>
Was it helpful?

Solution

Your image(s) is placed in tomcat at path /img/image.jpg that is context relative path, the absolute path is /MyProject/img/image.jpg for your tomcat, eventhough it is /img/image.jpg outside of the apache. You proxy / -> /MyProject so when you add the context name to the path 'MyProject', it really doesn't work as you mentioned.

SOLUTION 1:

Use context relative paths in tomcat

img/image.jpg

In this case you have to be careful about the requrested URI, e.g. /MyProject/page1/action1/ has its image relative path

../../img/image.jsp

SOLUTION 2:

Use document root paths with leading slash

/img/image.jpg

and define the element base with the document root ('href' attribute). Just be careful about link!

<head>
   <base href="http://www.mydomain.com/">
</head>

see http://www.w3schools.com/tags/tag_base.asp


SOLUTION 3:

Map the project to the same URI in apache as in tomcat (Personaly I use this solution as well because it is very easy, and I use a common word as a project/context name, e.g. 'web', 'site', etc.).

ProxyPass /MyProject ajp://localhost:8009/MyProject

SOLUTION 4:

Use a content filter such as mod_proxy_html http://httpd.apache.org/docs/current/mod/mod_proxy_html.html

NOTE: This solution is ever a bit slow (it doesn't matter witch filter you use)!


Be aware PROXY CONFIGURATION!!!

This is just about redirect etc., but you have a wrong configuration of your ProxyPathReverse!

ProxyPass / ajp://localhost:8009/MyProject
ProxyPassReverse /MyProject http://www.mydomain.com/

see the full explanation http://www.humboldt.co.uk/the-mystery-of-proxypassreverse/#more-131

read configuration examples http://www.apachetutor.org/admin/reverseproxies

OTHER TIPS

You need to either:

  1. Use mod_html to rewrite the links. This is slow, and an indication that you've done the wrong thing.

  2. Issue a redirect from / to /MyProject, which you can do with a RewriteRule, or a <meta http-equiv="refresh" content="0; url=http://<host>/MyProject/"> in /index.html, and change the ProxyPass directives to

    ProxyPass /MyProject ajp://localhost:8009/MyProject

so that proxying doesn't mess around with the URL paths. This is by far the better technique. You probably don't need the ProxyPassReverse directive at all, but if you do you should apply the same change.

I have not yet tested this for accuracy (and AJP tends to short circuit things like rewrites in Apache making extra testing and tweaking almost mandatory). So with that little AJP-disclaimer you might try something along the lines of:

ProxyPass /MyProject ajp://localhost:8009/MyProject
ProxyPassReverse /MyProject ajp://localhost:8009/MyProject
ProxyPass / ajp://localhost:8009/MyProject
ProxyPassReverse / ajp://localhost:8009/MyProject

Just to try catching those incorrect image paths on the inbound. If that fails try toying with a trailing slash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top