Configure Symlinks for single directory in Tomcat

2020-01-30 09:03发布

问题:

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.

回答1:

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. :-)



回答2:

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/



回答3:

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



回答4:

This works different in Tomcat 8+

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

<Resources allowLinking="true" />


回答5:

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.



回答6:

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



回答7:

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

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