Search code examples
proxydnsjettysubdomainjetty-9

Basic configuration to point a domain to a web application running on Jetty at port 8080


My current setup is very simple.

I have a webserver with a static ip running Ubuntu. I have a domain which points to that static ip correctly. On my server I have a Jetty instance running with standard / default configuration - more specifically - the etc/jetty-logging.xml and etc/jetty.xml configurations. Jetty currently listens on the default port 8080.

In my webapps folder I have two extracted web applications, so no war files.

My web applications are currently accessible as follows:

http://1.2.3.4:8080/web_app_1 and 
http://1.2.3.4:8080/web_app_2

http://www.example.com:8080/web_app_1 and
http://www.example.com:8080/web_app_2

What I am trying to achieve I imagine is quite simple but my experience with this is very limited.

I would like to serve one web application at http://www.example.com and the other at http://admin.example.com. So more specifically:

http://www.example.com -> http://1.2.3.4:8080/web_app_1
http://admin.example.com -> http://1.2.3.4:8080/web_app_2

What is the simplest and best way to achieve this?

I would prefer to not configure jetty for port 80 or have it run as root.

This is what I "think" I know so far...

I imagine I need to somehow forward traffic from port 80 to port 8080 internally on the webserver - I have no idea how to do this in the simplest possible way - I don't have too much configuration knowledge of apache or nginex so before I go ahead and use one of them I would like to know that I am following the correct route. I also know that one can use iptables but that route seems a bit too hidden and obscure for me.

I think I need to make use of Jetty's Virtual Hosts configuration which looks something like this (taken from https://wiki.eclipse.org/Jetty/Howto/Configure_Virtual_Hosts):

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="war"><SystemProperty name="jetty.home"/>/webapps/xxx.war</Set>
  <Set name="contextPath">/</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>333.444.555.666</Item>
      <Item>127.0.0.1</Item>
      <Item>www.blah.com</Item>
      <Item>www.blah.net</Item>
      <Item>www.blah.org</Item>
    </Array>
  </Set>
</Configure>
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="war"><SystemProperty name="jetty.home"/>/webapps/zzz.war</Set>
  <Set name="contextPath">/</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>777.888.888.111</Item>
      <Item>www.other.com</Item>
      <Item>www.other.net</Item>
      <Item>www.other.org</Item>
    </Array>
  </Set>
</Configure>

Unlike the example above I do not have a deployed war file and I am unable to find how to configure my web app directory instead with this configuration.

Any help or even a push in the right direction will be greatly appreciated!

Thanks!

Xandel


Solution

  • So I went the iptables route...

    The configuration ended up being extremely simple. As per the Jetty Documentation (http://www.eclipse.org/jetty/documentation/current/setting-port80-access.html) I configured iptables with the following command:

    /sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
    

    I then created a new xml configuration file for Jetty which looks as follows:

    <?xml version="1.0"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
    
    <Configure id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection">
      <Call name="addHandler">
        <Arg>
          <New class="org.eclipse.jetty.webapp.WebAppContext">
            <Set name="contextPath">/</Set>
            <Set name="extractWAR">false</Set>
            <Set name="copyWebDir">false</Set>
            <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/webapps/web_app_2/</Set>
            <Set name="virtualHosts">
               <Array type="java.lang.String">
                 <Item>admin.example.com</Item>
               </Array>
            </Set>
          </New>
        </Arg>
      </Call>
    
      <Call name="addHandler">
        <Arg>
          <New class="org.eclipse.jetty.webapp.WebAppContext">
        <Set name="contextPath">/</Set>
        <Set name="extractWAR">false</Set>
        <Set name="copyWebDir">false</Set>
            <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/webapps/web_app_1/</Set>
            <Set name="virtualHosts">
               <Array type="java.lang.String">
                 <Item>www.example.com</Item>
                 <Item>example.com</Item>
               </Array>
            </Set>
          </New>
        </Arg>
      </Call>
    </Configure>
    

    I hope this helps!

    Xandel