Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Tomcat server on two different ports

Tags:

java

port

tomcat

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?

like image 320
Narendra Avatar asked Mar 05 '13 18:03

Narendra


People also ask

Can Tomcat listen on multiple ports?

Is it possible for a single instance of Tomcat to listen on multiple ports? Sure, you can do it. Tomcat is support several protocol such as HTTP(80/8080) default, HTTPS.

How many ports does Tomcat use?

By default the tomcat binding ports are 8005, 8080 and 8009. If you have another tomcat instance running on same server or other application like JBoss Application Server, these ports are likely already used. In this case you should change the default ports.


3 Answers

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.

like image 194
Guillaume Fenollar Avatar answered Oct 14 '22 02:10

Guillaume Fenollar


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.

like image 32
Magnilex Avatar answered Oct 14 '22 03:10

Magnilex


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.

like image 19
Vinay Thube Avatar answered Oct 14 '22 01:10

Vinay Thube