83

How do you configure tomcat to bind to a single ip address (localhost) instead of all addresses?

ScArcher2
  • 85,501
  • 44
  • 121
  • 160

3 Answers3

146

Several connectors are configured, and each connector has an optional "address" attribute where you can set the IP address.

  1. Edit tomcat/conf/server.xml.
  2. Specify a bind address for that connector:
    <Connector 
        port="8080" 
        protocol="HTTP/1.1" 
        address="127.0.0.1"
        connectionTimeout="20000" 
        redirectPort="8443" 
      />
    
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
ScArcher2
  • 85,501
  • 44
  • 121
  • 160
  • 2
    In addition to **bind(2)** to a single address, you can also bind to more addresses by using multiple `Connector` elements. https://access.redhat.com/solutions/873953 – Low power Apr 20 '19 at 06:39
  • I am getting same issue can you pl check my [question](https://stackoverflow.com/q/75752438/6854117)? – Moeez Mar 16 '23 at 05:31
13

it's well documented here:

https://cwiki.apache.org/confluence/display/TOMCAT/Connectors#Connectors-Q6

How do I bind to a specific ip address? - "Each Connector element allows an address property. See the HTTP Connector docs or the AJP Connector docs". And HTTP Connectors docs:

http://tomcat.apache.org/tomcat-7.0-doc/config/http.html

Standard Implementation -> address

"For servers with more than one IP address, this attribute specifies which address will be used for listening on the specified port. By default, this port will be used on all IP addresses associated with the server."

f_puras
  • 2,521
  • 4
  • 33
  • 38
rnglbd
  • 471
  • 3
  • 6
  • I am getting same issue can you pl check my [question](https://stackoverflow.com/q/75752438/6854117)? – Moeez Mar 16 '23 at 05:51
3

It may be worth mentioning that running tomcat as a non root user (which you should be doing) will prevent you from using a port below 1024 on *nix. If you want to use TC as a standalone server -- as its performance no longer requires it to be fronted by Apache or the like -- you'll want to bind to port 80 along with whatever IP address you're specifying.

You can do this by using IPTABLES to redirect port 80 to 8080.

Hal50000
  • 639
  • 1
  • 6
  • 16