This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Tomcat and the fly likes Access static file from apache to tomcat web applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Access static file from apache to tomcat web applications " Watch "Access static file from apache to tomcat web applications " New topic
Author

Access static file from apache to tomcat web applications

Rithanya Laxmi
Ranch Hand

Joined: Jan 24, 2011
Posts: 136
Hi,


I have my static files loaded in c:/apache/htdocs of apache web server, I need to access that in my web application running in tomcat server.
For example

In c:/apache/htdocs, i have an images folder with static images which I am accessing in my web application

images
1.gif
2.jpg

In my web application i am accessing like
images/1.gif

But using the above I am unable to access the image from the Apache. Whether we need to integrate Apache & tomcat using
mod_jk, what are the other configuration we need to provide for this work?

In httpd.config I have added the below:-
<VirtualHost <IP address>:80>
DocumentRoot "c:/apache/htdocs"
/VirtualHost>

In tomcat web application I am accessing like:-
images/1.gif

When access the 1.gif in my webapplication that image is not displayed? Is there any other config I need to do in apache web server?

Thanks.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15962
    
  19

It's no longer considered a good idea to keep your static content under Apache and your dynamic content under Tomcat for simple performance reasons. If you need multiple apps to share these resources, that's different, but not because of some reason that became obsolete over a decade ago.

Please note that webapps don't "access" things like images. When you display a webpage with images, css, javascript, and/or other independent content, that content is not part of the webpage in the sense that the image data is embedded in the webpage datastream. Instead the image contains a URL that the client will then open and obtain the image data from (in fact, in the days of slower modems, it was common for browsers to disable automatic fetching of images).

That URL can point anywhere in the world. Not only does it not have to be within the Tomcat webapp, it doesn't have to be on the same server, or literally even in the same country. Look at almost any commercial webpage and you'll see URL references to a whole host of third-party contributors.

So all you really need is an independent Apache source for the static content. You can do that by setting up a separate Apache VirtualHost for the images (say, http://static.myhost.com/images/pic1.gif) or you can just do an Apache mod_rewrite to rewrite the image URL requests as Apache-hosted URLs.


Customer surveys are for companies who didn't pay proper attention to begin with.
Rithanya Laxmi
Ranch Hand

Joined: Jan 24, 2011
Posts: 136
Thanks. Could you please provide me the sample on setting up the virtual hosts to server static content from apache
In httpd.config, I have:-

NameVirtualHost <IP address>:80
<VirtualHost <IP address>:80>
DocumentRoot "c:/apache/htdocs\CSS"
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location>
Order allow,deny
Allow from all
</Location>
..

/VirtualHost>

In my web application running in tomcat:-

I am accessing the CSS as:-

<link href="CSS/style.css" rel="stylesheet" type="text/css" />

But I am unable do to so? Please clarify.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15962
    
  19

I can't give step-by-step details - at least not without sending you a bill - but there are a couple of general things you should be aware of.

First, there are (at least) 2 different ways to connect Apache to Tomcat. The older one is to use mod_jk. The newer one is to use mod_proxy. In either event, you define the URL pattern you want to use for requests sent on to Tomcat as part of the proxy configuration. For example, using mod_proxy:


That makes any request sent to myvirtualhost/tomcat go to Tomcat. If you want to, you can use "/" as the URL root pattern, and EVERYTHING will go to Tomcat.

If you set up URL rewriting, you can rewrite your image and CSS requests to go to local directories instead of being routed to the ProxyPass URL.
 
wood burning stoves
 
subject: Access static file from apache to tomcat web applications
 
Similar Threads
Request to Tomcat from remote machine
Accessing Remote Files
images don't appear outside local machine
jetty issue with images
Problems setting up VirtualHost to access a webapp running in a Tomcat/Apache environment