Running Tomcat server on two different ports
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?
Solution 1:
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.
Solution 2:
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.
Solution 3:
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 "%r" %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 "%r" %s %b" />
</Host>
</Engine>
</Service>
Note : You may have required to increase the tomcat heap size.
Solution 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>
Solution 5:
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