49

I want to to deploy a tomcat server such that it listens on two ports simultaneously (both for http protocol).

Just to make sure that you understand this requirement correclty , We have only one server instance but want to listen on two ports for HTTP protocol. For example anybody can access applications deployed in my server using port numbers 7080 and 8080

Is it possible to do that? If possible how can we achive this?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Narendra
  • 5,635
  • 10
  • 42
  • 54
  • Stranegly If I test with ports 8080,7080 it is working. If I test it with 80 and 8080 it is failing. Wondering why? – Narendra Mar 05 '13 at 18:40
  • 1
    The port 80 was already used by something else, could be an Apache Web Servier for example. – Magnilex Mar 05 '13 at 18:41
  • 1
    Yes you are right. For some strange reason my Skype using this port 80. Once I killed my Skype process the server is running fine. Thanks for your answer. – Narendra Mar 05 '13 at 18:45

6 Answers6

50

It's very simple. You only need to take a look at the conf/server.xml configuration file to add a new connector for the port you want. For example, if you have a connector like this:

<Connector port="8080" 
           protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" 
           URIEncoding="UTF-8" />

Just add a new connector same as above in the configuration file, but altering the port parameter. That's all. Restart and you're done.

  • 1
    java.lang.Exception: Socket bind failed: [730048] Only one usage of each socket address (protocol/network address/port) is normally permitted. – Narendra Mar 05 '13 at 18:30
  • 3
    @Narendra: Did you change both the parameter "port" and "redirectPort"? Which tomcat version are you using? – Magnilex Mar 05 '13 at 18:31
  • I found I didn't have to change the `redirectPort` parameter, provided I used different `port` values. – Duncan Jones May 16 '16 at 07:25
23

Yes, it is possible. Just edit server.xml (located in the folder named conf) like this:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />
<Connector port="8081" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8444" />

This will setup Tomcat to listen to both ports 8080 and 8081.

The documenation states:

  • port: The TCP port number on which this Connector will create a server socket and await incoming connections. Your operating system will allow only one server application to listen to a particular port number on a particular IP address. If the special value of 0 (zero) is used, then Tomcat will select a free port at random to use for this connector. This is typically only useful in embedded and testing applications.

  • redirectPort: If this Connector is supporting non-SSL requests, and a request is received for which a matching <security-constraint> requires SSL transport, Catalina will automatically redirect the request to the port number specified here.

So, altering the redirectPort is optional, depending on how you want such a redirection to work.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
19

You can define 2 different services in /conf/server.xml .

The example is as below,

<Service name="Catalina_2">
    <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8444" />
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />
    <Engine name="Catalina_2" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost"  appBase="webapps_2" 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>


  <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="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 &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>

Note : You may have required to increase the tomcat heap size.

Vinay Thube
  • 191
  • 1
  • 3
  • I like how this answer also isolates the log files, etc. for each port. That will be a huge benefit in troubleshooting when some error occurs. – Mark Stewart Jan 25 '21 at 20:12
4

you can specify the following code in your server.xml

<Service name="sample">

    <Connector port="81" protocol="HTTP/1.1" maxThreads="100" connectionTimeout="2000"/>

    <Engine name="sample" defaultHost="sample">
         <Host name="myhostname" appBase="webapp2">
             <Context docBase="C:\websites\sample\" />
         </Host>
     </Engine>

</Service>
sumit sharma
  • 1,067
  • 8
  • 24
  • I am getting following error: java.lang.Exception: Socket bind failed: [730048] Only one usage of each socket address (protocol/network address/port) is normally permitted. – Narendra Mar 05 '13 at 18:30
  • have you changed the port from 81 to your requirement. – sumit sharma Mar 05 '13 at 18:37
  • This is not working I am getting many exceptions in my console saying java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to initialize component [StandardEngine[sample].StandardHost[m yhostname].StandardContext[null]] ................. Caused by: org.apache.catalina.LifecycleException: A child container failed during start – Narendra Mar 05 '13 at 18:48
  • in that case you can remove the context tag an run again this will run properly. – sumit sharma Mar 05 '13 at 18:56
3

Please, be sure on which user you are running Tomcat, since if you want to use it on any privileged port, you must use it under the root user.

Another thing you can do is to redirect port 80 to 8080 with iptables. Something like this:

iptables -t nat -A PREROUTING -d 192.168.10.16 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080

Hope it helps

Cyberzoo
  • 131
  • 1
  • 2
    I don't see why not. Perhaps he needed to use another port below 1024 and just gave port 7080 as an example. Other answers where valid too, and this is another approach. – Cyberzoo Feb 25 '14 at 18:47
  • 4
    I like this answer, as it also speaks to potential solutions where iptables are a valid approach and editing a pre-configured server are not ( I have an improperly Dockerized app that has a problem this solves succinctly) – tjborromeo Sep 12 '14 at 04:10
3

running tomcat in different port. We have to change four things inside service tag of server.xml file

  1. we have to change port no. like 8080 to 80
  2. we have to change redirectPort no like 8443 to 8444
  3. we have to change Engine name like Catalina to Catalina_2
  4. we have to change appBase name like webapps to webapps_1
Tejpratap
  • 31
  • 1