Configure Symlinks for single directory in Tomcat
Asked Answered
O

7

29

I have a directory to which a process uploads some .pdf files. This process is out of my control.

I need to make those files available through the website using Tomcat.

I have a directory /var/lib/tomcat5/webapps/test1 available to the web and I can see the files in it with a browser.

So, I created a symbolic link pointing at the directory with the .pdf files: /var/lib/tomcat5/webapps/test1/files/, but I can't see anything in that directory.

How can I enable symlinks in the test1 directory only? I don't want to enable symlinks everywhere, just so that directory with .pdf files is available to the web.

Oneiric answered 24/11, 2008 at 19:5 Comment(0)
R
54

There are a few problems with the solution of creating a META-INF/context.xml that contains <Context path="/myapp" allowLinking="true">

The biggest issue is that if a conf/context.xml exists, the allowLinking in the <Context> there takes precedence over a <Context> in a META-INF/context.xml. And if the in the conf/context.xml does not explicitly define allowLinking, that's the same as saying allowLinking="false". (see my answer to a context precedence question)

To be sure that your app allows linking, you have to say <Context override="true" allowLinking="true" ...>.

Another issue is that the path="/myapp" is ignored in a META-INF/context.xml. To prevent confusion, it's best to leave it out. The only time path in a <Context> has any effect is in the server.xml, and the official Tomcat docs recommend against putting <Context>s in a server.xml.

Finally, instead of a myapp/META-INF/context.xml file, I recommend using a conf/Catalina/localhost/myapp.xml file. This technique means you can keep the contents of your META-INF clean, which is the guts of your webapp -- I don't like to risk mucking about in the guts of my webapp. :-)

Roseline answered 27/11, 2008 at 1:32 Comment(8)
big save.. thanks man. i had to put it into META-INF and add the override. (i think it doesn't work in the WEB-INF?)Whensoever
@piotr-czapla: Thanks for editing my answer to fix the WEB-INF to META-INF !Roseline
How would a files in this path be accessed from Java? E.g., if I wanted to create a File object to /var/lib/tomcat5/webapps/test1/files/example.pdf what would be the path string given to the constructor?Inexpert
@beldaz: You should create a new question (stackoverflow.com/questions/ask) rather than using comments.Roseline
@Roseline it's fine, I was asking so that you could make your answer more complete, since many similar questions omit this small issue that confuses noobs like me. Files are simply accessed by their full file system path.Inexpert
Where is the conf/context.xml file when not in the META-INF/context.xml location? Is it in ..../content/[WEBAPP]/conf/context.xml? or off catalina or tomcat or something?Nostrum
@DaveX: Regarding locations that are searched for context.xml and their precedence, see my answer to a context precedence question that I mentioned in my original answer.Roseline
this answer still allows any symbolic link found in the resources directory, so it doesn't answer the original question of "I don't want to enable symlinks everywhere, just so that directory with .pdf files is available"Diarrhoea
W
17

Create a context.xml file in a META-INF directory in your web app containing:

<?xml version="1.0" encoding="UTF-8"?>

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

</Context>

more here: http://www.isocra.com/2008/01/following-symbolic-links-in-tomcat/

Wove answered 24/11, 2008 at 19:21 Comment(2)
Yes, all I had to do was create a context.xml file : /var/lib/tomcat5/webapps/test1/META-INF/context.xml That link was very helpful. Thank you.Oneiric
Thanks for this. Any idea how to configure tomcat (6) to always follow symlinks?Brasil
H
9

This works different in Tomcat 8+

http://tomcat.apache.org/migration-8.html

<Resources allowLinking="true" />
Hiss answered 24/2, 2018 at 12:15 Comment(0)
N
8

Yes I know it's an old question, but I found a new solution, using mount with the --bind option instead of a symlink, and tomcat doesn't need any reconfiguring:

cd /var/lib/tomcat5/webapps/test1/

mkdir files

mount --bind /path/to/actual/upload/directory/files files

Needlecraft answered 8/6, 2015 at 14:50 Comment(0)
A
4

There are 4 places where Context can live.

  1. tomcatdir/conf/server.xml
  2. tomcatdir/conf/context.xml
  3. tomcatdir/conf/Catalina/localhost/appname.xml
  4. tomcatdir/webapps/appname/META-INF/context.xml

In case of tomcat 8 allowlinking attribute should be specified not in Context but in Resources tag. My tomcatdir/conf/context.xml looks like this

<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
 <Resources allowLinking="true" cachingAllowed="true" cacheMaxSize="100000" />
</Context>

This solution works fine for me now. But I want to share also the mistake I had done before coming to this solution.

I had defined Resources both in tomcatdir/conf/server.xml and in tomcatdir/conf/context.xml. And allowLinking="true" was set only in tomcatdir/conf/server.xml.

What I found was that if you do not specify allowLinking it is equal to setting it to false. So I removed Resources tag from server.xml and left it only tomcatdir/conf/context.xml with the allowLinking="true" attribute in it.

Akins answered 28/4, 2017 at 6:30 Comment(0)
M
1

I made it in this other way. I edit this other configuration file: apache-tomcat-7.0.33/conf/server.xml In Host tag I added:

<Context path="/data" docBase="C:\datos" debug="0" reloadable="true" crossContext="false"/>

So, you can acces via: http://localhost/data

Mag answered 21/5, 2015 at 8:0 Comment(0)
L
0

Adding following line into conf/context.xml enables softlinks for me on apache tomcat 8.5+

<Resources allowLinking="true" cachingAllowed="true" cacheMaxSize="100000">

Lyndes answered 27/11, 2018 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.