Pete Freitag Pete Freitag

Tomcat Virtual Directory Howto

Updated on August 08, 2023
By Pete Freitag
coldfusionjava

Let's create a Tomcat virtual directory (or Alias) called /my_scripts which points x:\cf\cfusion\wwwroot\cf_scripts\scripts\ by adding the following inside of a <Host> tag in the server.xml file:

<Context path=""
   docBase="x:/cf/cfusion/wwwroot"
   WorkDir="x:/cf/cfusion/runtime/conf/Catalina/localhost/tmp">
  <Resources>
    <PreResources base="x:/cf/cfusion/wwwroot/cf_scripts/scripts"
        className="org.apache.catalina.webresources.DirResourceSet"
        webAppMount="/my_scripts"/>
  </Resources>
</Context>

A few things to note:

  • Take a backup of server.xml first, if you end up with invalid XML or configuration the server will not start up again until you fix it.
  • You will need to restart Tomcat / ColdFusion for the change to apply
  • Note that I am using / forward slashes in the paths to avoid escaping backslashes.
  • Assumes my tomcat webroot is x:/cf/cfusion/wwwroot
  • Assumes my tomcat temp directory is: x:/cf/cfusion/runtime/conf/Catalina/localhost/tmp
  • Assumes I'm using Tomcat 8.5 or greater (eg CF2016+)
  • You can find the server.xml file in the conf folder of a Tomcat install, or the runtime/conf folder of your ColdFusion instance. So using the example paths above the file would be located in x:/cf/cfusion/runtime/conf/server.xml (thanks Charlie for the suggestion to add this)

The syntax for creating a virtual directory in Tomcat changed in Tomcat 8.5, if you are using Tomcat 8, Tomcat 7 (CF10 or CF11) or below you will need to use a syntax like this:

<Context path=""
   docBase="x:/cf/cfusion/wwwroot"
   WorkDir="x:/cf/cfusion/runtime/conf/Catalina/localhost/tmp"
   aliases="/my_scripts=x:/cf/cfusion/wwwroot/cf_scripts/scripts">


tomcat

Tomcat Virtual Directory Howto was first published on July 03, 2019.


Fixinator

The Fixinator Code Security Scanner for ColdFusion & CFML is an easy to use security tool that every CF developer can use. It can also easily integrate into CI for automatic scanning on every commit.


Try Fixinator

CFBreak
The weekly newsletter for the CFML Community


Comments

Thank you very much for this bit; it helps a lot if you erraneously have changed the script directory in cfadmin, forgetting that the admin uses the built in webserver and not iis ... ;-)
by Ralf_G on 11/12/2019 at 7:40:58 PM UTC
Great post as always, Pete. But I was helping someone who tripped over something not mentioned here.

Note that the value shown here for the "base" path, with it ending in /scripts, will NOT be correct if your CF Admin setting for "default script src" is itself still showing use of something/scripts. Let me clarify.

As you discuss on your post about changing the default script src value, the default (in 2016 and above) looks like cf_scripts/scripts. And you mention there that one could rename it, such as changing it to cfjs, or here you discuss changing it to my_scripts, and in both cases you would point the virtual directory to what you show:

base="x:/cf/cfusion/wwwroot/cf_scripts/scripts"

But again if the value that a user has in the cf admin still includes /scripts on the end (like if they changed it to /cfjs/scripts), then this value above should NOT end in /scripts, otherwise the calls that CF generates for the affected tags will get 404, because they ask for their value (such as /cfjs/scripts) but then this "base" value in the context above is pointing to to scripts...and there's no "scripts" underneath the "scripts" folder.

So bottom line, if someone sees their CF admin with some value in the default script src that ends in /scripts, then they do NOT want to specify here that /scripts on the end of the base path. Instead, it should be:

base="x:/cf/cfusion/wwwroot/cf_scripts"

As a last point to help folks doing any of this, you would help yourself to open your browser dev tools feature, and use its "network" tab, to see the various URLs that CF will be telling the browser to come get. If there are 404s, you will see that, and you will see the path they are trying to visit. You can then perhaps better connect the dots of what I am saying here.

Hope that's helpful.

PS As Pete notes, whenever you edit this server.xml, you want to be sure to back it up before changing it. If you make any mistake in this file, you may find CF comes up but no pages work, if there's any error in the server.xml, like simply a missing closing bracket, etc.
by Charles Arehart on 09/05/2020 at 7:13:19 PM UTC
Here's another tip: this post doesn't let people know where to find that server.xml that needs to be edited. It's in the runtime/conf/ under the cfusion folder (or if you have another instance, under that instance's folder). (I often help people by pointing them to these great posts, to serve as their documentation of what needs to be done, and that's how I notice these little opportunities for improvement, as I work with different people depending on their level of experience with all this sort of stuff.)

Pete, if you may be able to add that clarification to the post, I'd be ok with you deleting this post, as it will have served its purpose. Until then, perhaps it may help some.
by Charles Arehart on 09/19/2020 at 4:18:13 AM UTC