在*:<port>而不是localhost:<port>上运行Tomcat

问题描述

我正在尝试从Parallels Desktop访问我的本地主机服务器,并且遇到了一些问题。

例如,我在Spring Boot上有一个运行在端口8081上的应用程序,并且在Terminal中运行sudo lsof -PiTCP -sTCP:LISTEN会给我以下输出

java 8168 username 107u IPv6 0x53524f3d71f26ae5 0t0 TCP *:8081 (LISTEN)

第二个应用程序(不是Spring Boot,而是Tomcat 7中的Spring)的输出为:

java 7756 username 504u IPv6 0x53524f3d6cfe6fa5 0t0 TCP localhost:8096 (LISTEN)

我可以通过地址http://10.211.55.2:8081/从Parallels Desktop轻松访问第一个应用程序,但是对http://10.211.55.2:8096/进行相同操作会使我无法访问此网站。

那么,如何在*:8096而不是localhost:8096上运行第二个应用程序?

解决方法

tomcat侦听的IP地址由tomcat配置文件server.xml中的address attribute in the connector element控制。

要监听所有地址,请将地址设置为0.0.0.0

<Connector port="8096" 
           address="0.0.0.0"
           ...other attributes.../>