在两个不同的端口上运行Tomcat服务器

时间:2021-05-03 18:11:39

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

我想部署一个tomcat服务器,以便它同时侦听两个端口(两者都用于http协议)。

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

为了确保您了解此要求,我们只有一个服务器实例,但希望在两个端口上侦听HTTP协议。例如,任何人都可以使用端口号7080和8080访问部署在我的服务器中的应用程序

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

有可能吗?如果可能的话,我们怎么能做到这一点?

6 个解决方案

#1


41  

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:

这很简单。您只需要查看conf / server.xml配置文件,为所需的端口添加新连接器。例如,如果您有这样的连接器:

<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.

只需在配置文件中添加与上述相同的新连接器,但需要更改端口参数。就这样。重启,你就完成了。

#2


20  

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

对的,这是可能的。只需编辑server.xml(位于名为conf的文件夹中),如下所示:

<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.

这将设置Tomcat以侦听端口8080和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.

    port:此连接器将在其上创建服务器套接字并等待传入​​连接的TCP端口号。您的操作系统将只允许一个服务器应用程序侦听特定IP地址上的特定端口号。如果使用特殊值0(零),则Tomcat将随机选择一个空闲端口用于此连接器。这通常仅适用于嵌入式和测试应用程序。

  • 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.

    redirectPort:如果此Connector支持非SSL请求,并且收到匹配的 需要SSL传输的请求,则Catalina将自动将请求重定向到此处指定的端口号。

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

因此,根据您希望这种重定向的工作方式,更改redirectPort是可选的。

#3


15  

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

您可以在/conf/server.xml中定义2个不同的服务。

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.

注意:您可能需要增加tomcat堆大小。

#4


3  

you can specify the following code in your server.xml

您可以在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>

#5


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.

请确保您运行Tomcat的用户,因为如果要在任何特权端口上使用它,则必须在root用户下使用它。

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

您可以做的另一件事是使用iptables将端口80重定向到8080。像这样的东西:

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

Hope it helps

希望能帮助到你

#6


2  

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

在不同的端口运行tomcat。我们必须在server.xml文件的服务标签内更改四件事

  1. we have to change port no. like 8080 to 80
  2. 我们要改变港口号码。像8080到80
  3. we have to change redirectPort no like 8443 to 8444
  4. 我们必须将redirectPort改为8443改为8444
  5. we have to change Engine name like Catalina to Catalina_2
  6. 我们必须将像Catalina这样的Engine名称更改为Catalina_2
  7. we have to change appBase name like webapps to webapps_1
  8. 我们必须将webapps等appBase名称更改为webapps_1

#1


41  

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:

这很简单。您只需要查看conf / server.xml配置文件,为所需的端口添加新连接器。例如,如果您有这样的连接器:

<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.

只需在配置文件中添加与上述相同的新连接器,但需要更改端口参数。就这样。重启,你就完成了。

#2


20  

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

对的,这是可能的。只需编辑server.xml(位于名为conf的文件夹中),如下所示:

<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.

这将设置Tomcat以侦听端口8080和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.

    port:此连接器将在其上创建服务器套接字并等待传入​​连接的TCP端口号。您的操作系统将只允许一个服务器应用程序侦听特定IP地址上的特定端口号。如果使用特殊值0(零),则Tomcat将随机选择一个空闲端口用于此连接器。这通常仅适用于嵌入式和测试应用程序。

  • 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.

    redirectPort:如果此Connector支持非SSL请求,并且收到匹配的 需要SSL传输的请求,则Catalina将自动将请求重定向到此处指定的端口号。

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

因此,根据您希望这种重定向的工作方式,更改redirectPort是可选的。

#3


15  

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

您可以在/conf/server.xml中定义2个不同的服务。

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.

注意:您可能需要增加tomcat堆大小。

#4


3  

you can specify the following code in your server.xml

您可以在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>

#5


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.

请确保您运行Tomcat的用户,因为如果要在任何特权端口上使用它,则必须在root用户下使用它。

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

您可以做的另一件事是使用iptables将端口80重定向到8080。像这样的东西:

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

Hope it helps

希望能帮助到你

#6


2  

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

在不同的端口运行tomcat。我们必须在server.xml文件的服务标签内更改四件事

  1. we have to change port no. like 8080 to 80
  2. 我们要改变港口号码。像8080到80
  3. we have to change redirectPort no like 8443 to 8444
  4. 我们必须将redirectPort改为8443改为8444
  5. we have to change Engine name like Catalina to Catalina_2
  6. 我们必须将像Catalina这样的Engine名称更改为Catalina_2
  7. we have to change appBase name like webapps to webapps_1
  8. 我们必须将webapps等appBase名称更改为webapps_1