Monday, August 20, 2007

Tomcat and Symbolic Links

I didn't expect Tomcat would really care about symbolic links. Say you put together a web application in a directory and want to use a web.xml you have elsewhere. Instead of copying web.xml to the WEB-INF directory, you create a symbolic link.

You would expect this work out of the box, wouldn't you? Well, no. For security reasons Tomcat won't follow your symbolic links, unless you specify on the Context of your web application allowLinking="true", as in:

<context path="/my-app" docBase="/Users/avernet/my-app"
allowLinking="true"/>

14 comments:

Anonymous said...

That little snippet just saved me a very painful experience. Thanks.

Alessandro Vernet said...

Thank you for the comment. I am glad this was helpful.

Alex

Viru said...

Which file did you put this in? I have tried changing conf/context.xml but it doesn't work? I have done it successfully before but now i don't remember which file I changed.

Help much appreciated,
Thanks

Alessandro Vernet said...

Viru,

This is in server.xml. See the documentation for the the Context element in Tomcat.

Alex

Viru said...

That worked. I needed to provide symbolic links to images and videos that get uploaded by the users. I am using tomcat 6.0.14. I added the following to tomcat/conf/server.xml in the node <Server><Service><Engine><Host>

<Context path="/myapp" docBase="myapp" allowLinking="true"/>

Thanks Alex.

Alessandro Vernet said...

Viru, my mistake: I didn't use an uppercase B in docBase in my code above. I fixed that. Thank you for letting me know.

Anonymous said...

It's been a year since the last post on this blog but I figure I'll give it a shot since it's right on the problem I'm having.

This little code snippet works (almost) perfectly for me except that Tomcat won't follow nested symbolic links. I set it up exactly like in your example and all the files get loaded perfectly. However, if I create a symbolic link in the subfolder (which tomcat already links to) tomcat doesn't follow it and claims a 404 when I try to locate the resource. It seems tomcat only goes into 1 symbolic link and from there does not continue linking. Any help on this would be appreciated.

Alessandro Vernet said...

Are those symbolic links on UNIX or OS X? I.e. shortcuts on Windows won't work, AFAIK. I can't think of anything else that could go wrong.

Alex

Anonymous said...

Thanks for the response Alessandro. I should've mentioned it is Linux. Ubuntu Hardy. Latest tomcat and jvm 1.6.
Basically I want to build some kind of media repository which I can add and remove folders to. Eg. I might add a link to a folder for images, another for movies etc.
I can add another context but then I have to edit the server.xml, restart tomcat etc. It would be effortless to simply add a symbolic link in a folder and that it is picked up by tomcat right away.

Regards

Alessandro Vernet said...

If you are on Linux you should be good. I am reading again the documentation for this attribute, and I don't see why it wouldn't work for you. I am really using this all the time, creating a "virtual war" by linking different pieces "into the war", so I can still keep and edit those pieces in their original location. You might want to ask the question on the tomcat-user list (tomcat-user on Nabble).

Alex

Prashant said...

Thanks. Saved the pain!

Anonymous said...

Very Grateful.

This setting is so crucial to intranet environments where you dont want to duplicate data to your web server.

I'm running OpenBlueDragon on Tomcat.

so I added this line to /etc/tomcat6/server.xml

<Context path "/openbd" allowLinking="true" />

NB. inside the <Host...> at the end of the file.

cheers, Jonathan

Anonymous said...

Thanks for posting this. You saved me a bunch of time.

Vineet mishra said...

Thanks a lotttttttttttttttttt
It worked like a rocket ...
(I was changing the links and all many ways .. spent too much time before I read this blog)