How to route traffic from Nginx ingress to an application Tomcat server

1/8/2020

I have a Kubernetes cluster with an (kubernetes) Nginx Ingress controller with the following ingress rules:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-rules
#  namespace: default

### Virtual hosts ###
spec:

  rules:

  - host: dashboard.example.com
    http:
      paths:
        - path: /
          backend:
            serviceName: test
            servicePort: 443

The test application is served by a Tomcat server.

The server.xml for the Tomcat is:

<?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"
        URIEncoding="UTF-8"
               redirectPort="443" />

    <Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
        sslImplementationName="org.apache.tomcat.util.net.jsse.JSSEImplementation"
        scheme="https" secure="true" URIEncoding="UTF-8"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/odvsdva.p12"
        certificateKeystorePassword="sdvsdvsdvsd"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>

    <Connector port="8009" protocol="AJP/1.3" redirectPort="443" />

    <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="webapps" unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />

      </Host>

    </Engine>
  </Service>
</Server>

When I open in a browser dashboard.example.com when the path in the Ingress is / , I get the Tomcat standard status page (If you're seeing this, you've successfully installed Tomcat. Congratulations!).

Now, if I go to dasboard.example.com/test I can open my app.

However, I would like to open the app just by navigating to dashboard.example.com/.

So, I try changing the path in the Ingress from / in /test. But then I get This site can’t be reached in the browser.

I did not setup Tomcat and I have very little know-how on it. I hope I provide enough details above.

EDIT

My k8s cluster is inside an Ubuntu vagrant VM.

root@vagrant:/home/vagrant# kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.4", GitCommit:"67d2fcf276fcd9cf743ad4be9a9ef5828adc082f", GitTreeState:"clean", BuildDate:"2019-09-18T14:51:13Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.7", GitCommit:"6c143d35bb11d74970e7bc0b6c45b6bfdffc0bd4", GitTreeState:"clean", BuildDate:"2019-12-11T12:34:17Z", GoVersion:"go1.12.12", Compiler:"gc", Platform:"linux/amd64"}
-- Kostas Demiris
kubernetes
nginx
tomcat

2 Answers

1/8/2020

I believe the thing you are looking for is nginx.ingress.kubernetes.io/rewrite-target but in order to insert a path component rather than removing it as in their example:

kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /test$1
spec:
  rules:    
  - host: dashboard.example.com
    http:
      paths:
        - path: '(/.*)'
-- mdaniel
Source: StackOverflow

1/8/2020

Keep path in the Ingress as /

Remove the out-of-the-box ROOT/ directory from tomcat and rename your war file to ROOT.war before deploying it.

-- Arghya Sadhu
Source: StackOverflow