user1361424
user1361424

Reputation:

How to get Apache Tomcat to serve my webpage from 'www.myapp.com' rather than 'www.myapp.com/myapp'?

Sorry, I couldn't think of a more descriptive title without explaining the whole situation.

I am using IIS7 with Tomcat7. I created a web app using GWT, packaged it into a war file and deployed it on Tomcat.
Typing in my website url, say "www.myapp.com", I am shown the Tomcat default page. I suspect this is because thats whats in the %TOMCAT_HOME%/webapps/ROOT directory. I can access my webapp by "www.myapp.com/myapp" and all the functionality I'm expecting is there.

How do I configure Tomcat so that my webapp can be accessible through "www.myapp.com" rather than "www.myapp.com/myapp"?

I've tried configuring server.xml in %TOMCAT_HOME%/conf by adding a new 'host' tag for my webapp:

        <Host name="myapp.com" debug="0" appBase="webapps/myapp" unpackWARs="true">
              <Alias>www.myapp.com</Alias>
              <Context path="" docBase="." debug="0" reloadable="true"/> 
        </Host>

Adding the Host tag from above, typing in "www.myapp.com" takes me directly to my web application now. The problem is that when I do something in the application that makes a call to the servlet(I am using GWT RPC), I get errors:

com.google.gwt.user.client.rpc.StatusCodeException: 404

Server Error 404 - File or directory not found.

The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.

I don't know why I am getting this error, when typing in "www.myapp.com/myapp" before without the added 'Host' tags worked fine.

I am new to Tomcat, and am not sure how to go about solving this. Any help would be greatly appreciated. Thanks

EDIT:

a) Hosts sections from server.xml (I have the default host as www.myapp.com):

<Host name="localhost"  appBase="C:/Tomcat 7.0/webapps" unpackWARs="true" autoDeploy="true"></Host>


<Host name="www.myapp.com" appBase="C:/Tomcat 7.0/myapp" unpackWARs="true" autoDeploy="true">       </Host>

b) workers.properties (I've tried changing the host here to www.myapp.com to no avail):

worker.list=worker1
worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13

c) uriworkermap.properties:

/*=worker1

Upvotes: 2

Views: 6596

Answers (2)

Mark Thomas
Mark Thomas

Reputation: 16615

You have two options.

  1. Rename your WAR to ROOT.war.

  2. Move your WAR outside of Tomcat's appBase and then add a new file $CATALINA_BASE/Catalina/localhost/ROOT.xml with the following contents:

    <Context docBase="path/to/WAR" />

This assumes that you are using the default names for the Engine and Host. If not, adjust the path accordingly. It is $CATALINA_BASE/<engine_name>/<host_name>/ROOT.war

Now you are using multiple hosts with option 1, you need to do the following since appBase != docBase. If try and use the same value for them, all sorts of bad stuff will happen.

There are several ways to get to a working solution from what you have. I suggest the following: 1. Create a directory called C:/Tomcat 7/webapps-myapp/ROOT 2. Copy the contents of C:/Tomcat 7/myapp to this new directory 3. Remove C:/Tomcat 7/myapp 4. Change the appBase for the MyApp host to "webapps-myapp" (or the full path)

That will deploy the application from C:/Tomcat 7/myapp as the ROOT (default) application in the myapp virtual host. This assumes that you have deployed the myapp application to C:/Tomcat 7/myapp

Upvotes: 3

paulsm4
paulsm4

Reputation: 121609

From the Tomcat 7 docs:

http://tomcat.apache.org/tomcat-7.0-doc/config/host.html

If you are using the standard Host implementation, the following actions take place automatically when Catalina is first started, if the deployOnStartup property is set to true (which is the default value):

Any XML file in the Host's xmlBase directory (by default $CATALINA_BASE/conf/[engine_name]/[host_name]) is assumed to be a

context XML descriptor containing a Context element (and its associated sub-elements) for a single web application. The web applications associated with each of these context XML descriptor files will be deployed first.

The docBase attribute of this <Context> element must only be set if the docBase is outside the Host's appBase. For web applications

located inside the Host's appBase, the docBase will be the name of the XML file with ".xml" replaced with ".war" for a web application archive or the name of the XML file with ".xml" removed for a directory.

The path attribute must not be set. The context path used will be a slash character ("/") followed by the name of the XML file (less the

.xml extension). Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application that has a context path of / may be defined by using a file called ROOT.xml.

Any web application archive file within the Host's appBase directory that has not already been deployed as a result of a context

XML descriptor, does not have a corresponding directory of the same name (without the ".war" extension), and is not excluded by deployIgnore will be deployed next. The context path used will be a slash character ("/") followed by the web application archive name less the ".war" extension. The one exception to this rule is that a web application archive named "ROOT.war" will be deployed with a context path of /. Multi-level contexts may be defined by using #, e.g. use a WAR named foo#bar.war for a context path of /foo/bar.

If the unpackWARs attribute is true, the web application archive file will be expanded to a directory of the same name (without the

".war" extension".

Note: If you re-deploy an updated WAR file while Tomcat is stopped, be sure to delete the associated expanded directory before

restarting Tomcat, so that the updated WAR file will be re-expanded when Tomcat restarts.

If copyXML is true (it is false by default), any web application archive file within the Hosts's appBase directory that does not have a

corresponding context XML descriptor (with a ".xml" extension rather than a ".war" extension) in the Host's xmlBase will be scanned to see if it contains a context XML descriptor (located at /META-INF/context.xml) and if one is found the descriptor will be copied to the xmlBase directory and renamed.

Finally, any sub-directory within the Host's appBase that has not already been deployed as a result of a context XML descriptor and is

not excluded by deployIgnore will be deployed. The context path used will be a slash character ("/") followed by the directory name, unless the directory name is ROOT, in which case the context path will /. Multi-level contexts may be defined by using #, e.g. use a directory named foo#bar for a context path of /foo/bar.

If copyXML is true (it is false by default), any directory within the Hosts's appBase directory that does not have a corresponding

context XML descriptor in the Host's xmlBase will be scanned to see if it contains a context XML descriptor (located at /META-INF/context.xml) and if one is found the descriptor will be copied to the xmlBase directory and renamed.

Main point in all this:

a web application archive named "ROOT.war" will be deployed with a context path of /.

Upvotes: -1

Related Questions