Running Tomcat server on two different ports
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
Related videos on Youtube
Narendra
Updated on July 09, 2022Comments
-
Narendra almost 2 years
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?
-
Narendra about 11 yearsStranegly If I test with ports 8080,7080 it is working. If I test it with 80 and 8080 it is failing. Wondering why?
-
Magnilex about 11 yearsThe port 80 was already used by something else, could be an Apache Web Servier for example.
-
Narendra about 11 yearsYes 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 about 11 yearsI 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 about 11 yearsjava.lang.Exception: Socket bind failed: [730048] Only one usage of each socket address (protocol/network address/port) is normally permitted.
-
Magnilex about 11 years@Narendra: Did you change both the parameter "port" and "redirectPort"? Which tomcat version are you using?
-
sumit sharma about 11 yearshave you changed the port from 81 to your requirement.
-
Narendra about 11 yearsThis 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
-
sumit sharma about 11 yearsin that case you can remove the context tag an run again this will run properly.
-
Cyberzoo over 10 yearsI 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.
-
tjborromeo over 9 yearsI 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)
-
Duncan Jones about 8 yearsI found I didn't have to change the
redirectPort
parameter, provided I used differentport
values. -
Mark Stewart over 3 yearsI 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.