title: Running Apache and Tomcat on guest VM, Tomcat redirects to host
body: <p>I'm trying to deploy my webapp on a Vagrant VM and am having issues with Tomcat redirects and ports.</p>
  
  <p>Here's the configuration:</p>
  
  <ul>
  <li><p>Guest OS is Cent OS 6.3 running on Virtualbox.</p></li>
  <li><p>Apache 2.4 is installed and running on guest with host port 4000
  being forwarded to guest port 80.</p></li>
  <li><p>Tomcat 7 is installed and running on guest with host port 4040 being
  forwarded to guest port 8000 (for JPDA debugger).</p></li>
  <li><p>mod_jk is installed to forward requests from Apache to Tomcat.</p></li>
  </ul>
  
  <p>For the most part, it works. I have one webapp deployed at /mywebapp, which I can access by entering <a href="http://localhost:4000/mywebapp" rel="nofollow">http://localhost:4000/mywebapp</a> into my <strong>host</strong> browser. The problem occurs when the mywebapp application does a credentials check, and issues a redirect if there is no logged in user.</p>
  
  <p>What happens at this point is that my <strong>host</strong> browser URL is changed to <strong><a href="https://localhost" rel="nofollow">https://localhost</a></strong>/mywebapp/login.jsp instead of <strong><a href="https://localhost:4000" rel="nofollow">https://localhost:4000</a></strong>/mywebapp/login.jsp, which causes me to get an error since I don't have Apache running on my host machine.</p>
  
  <p>I know that mod_jk is working, because I can debug the mywebapp application and follow it to the point where the forward actually happens, so I know that the request is successfully being forwarded <strong>to</strong> Tomcat. Because of this I believe the problem lies with Tomcat and not Apache.</p>
  
  <p>Here is my server.xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Server port="8005" shutdown="SHUTDOWN"&gt;
      &lt;Listener className="org.apache.catalina.startup.VersionLoggerListener"/&gt;
      &lt;Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/&gt;
      &lt;Listener className="org.apache.catalina.core.JasperListener"/&gt;
      &lt;Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/&gt;
      &lt;Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/&gt;
      &lt;Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/&gt;
  
      &lt;GlobalNamingResources&gt;
          &lt;Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/&gt;
      &lt;/GlobalNamingResources&gt;
  
      &lt;Service name="Catalina"&gt;
  
          &lt;Connector 
              connectionTimeout="20000" 
              port="8080" 
              protocol="HTTP/1.1" 
              redirectPort="8443" /&gt;
  
          &lt;!-- Define an AJP 1.3 Connector on port 8009 --&gt;
          &lt;Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/&gt;
  
          &lt;Engine defaultHost="localhost" name="Catalina"&gt;
              &lt;Realm className="org.apache.catalina.realm.LockOutRealm"&gt;
  
                  &lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/&gt;
              &lt;/Realm&gt;
  
              &lt;Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"&gt;
  
                  &lt;Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &amp;quot;%r&amp;quot; %s %b" prefix="localhost_access_log." suffix=".txt"/&gt;
  
              &lt;/Host&gt;
          &lt;/Engine&gt;
      &lt;/Service&gt;
  &lt;/Server&gt;
  </code></pre>
  
  <p>If there are any other config files I need to share please let me know.</p>
  
  <p>If I ssh into the guest VM manually and run </p>
  
  <pre><code>wget localhost/mywebapp
  </code></pre>
  
  <p>it returns the page as expected. I'm not sure what that means.</p>