skip to Main Content

I have a trouble with configure of Apache and Tomcat. My purpose is to get ability to adress to tui.org/servlet, when tui.org is my virtual host configured on local machine and /servlet/ is url-pattern which is defined at web.xml. But instead the servelet I get 404 Not Found by Tomcat but I can to adress to my server by tui.org/tui.org/servlet – it works. Structure of files on my server is the following:

Root: /var/www/tui.org
-------->/WEB-INF/classes/a
-------->/WEB-INF/classes/a/MainServlet.class
-------->/WEB-INF/web.xml
-------->/index.html

How can I achieve this behaviour?

web.xml:

<servlet-mapping>
    <servlet-name>mainServlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
</servlet-mapping>

My server.xml:

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

<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="/var/www/"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>
</Server>

Configuration of tui.org (tui.org.conf at /etc/apache2/sites-available:

<VirtualHost *:80>
    ServerName tui.org
    DocumentRoot /var/www/tui.org

    <Directory /var/www/tui.org>
        AllowOverride All
    Require all granted
    </Directory>

    JkMount /servlet* ajp13_worker
</VirtualHost>

I use Apache2, tomcat9 and Ubuntu 18.04.

Thank you in advance!

P.S. Sorry for my english.

2

Answers


  1. There are several issues here:

    • You must not serve the web application directory with Apache – e.g. you’re declaring DocumentRoot /var/www/tui.org for Apache and <Host appBase="/var/www/"> for Tomcat.
      • e.g but not limited to: The servlet spec specifically disallows to serve the directory WEB-INF to ever be served to a client, and you’re explicitly bypassing this limitation.
    • An application deployed in a Host‘s appBase is always deployed under that name (unless specified differently in a context.xml file). The only other exception to that rule is if a directory is called ROOT – in that case, it’s deployed without a name, thus omitting the tui.org that follows your server name.
      • but this is secondary to the issue of serving the whole webapp through Apache httpd. Fix that one first.

    One way to fix your biggest problem is to omit the DocumentRoot directive in Apache and rather serve everything from Tomcat: JkMount * ajp13_worker.

    Login or Signup to reply.
  2. For solving problem with adressing by tui.org/servlet I had to add <Context> directive to server.xml:

    <Context path="" docBase="/var/www/tui.org/"/>
    

    and change appBase from /var/www/ to /var/www/tui.org/.

    And for fix problem with access to WEB-INF (thanks to @OlafKock) I had to add next to tui.org.conf:

    <Directory /var/www/tui.org>
        AllowOverride All
    Deny from all
    </Directory>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search