How to host multiple sites in Tomcat?

Is it possible to host multiple sites with Tomcat? Each site should have its own domain name and "/" context. I could not find documentation / instructions on this.

+4
source share
2 answers

Definitely possible, and very easy. The main configuration file for Tomcat conf / server.xml can contain several elements, each of which contains its own settings and Webapp definitions. Tomcat will respond to incoming requests to these hosts (host aliases are also supported) and redirect the request to the correct web address.

So, as a rule, you should add an ad with the base application directory for a specific host, and then release the WAR in this directory and work.

See Container host for a specific host configuration.

+8
source

However, another possibility is to use tomcat as a dispatcher / proxy for other applications in different cats or in the same one.

For this, you are advised to use UrlRewriteFilter

Create a new web project called ROOT, integrate UrlRewriteFilter and deploy it to your tomcat. (Download the commons-codec and commons-httpclient libraries if you intend to use it as a proxy)

Edit urlrewrite.xml and create your own routing there.

 <rule> <condition name="host" operator="equal">mydomain1.com</condition> <from>(.*)</from> <to type="proxy">http://localhost:8080/MyDomain1/index.jsp?$1</to> </rule> <rule> <condition name="host" operator="equal">mydomain2.com</condition> <from>(.*)</from> <to type="proxy">http://localhost:8090/MyDomain2/index.jsp?$1</to> </rule> 

* Remember to "enter" according to your needs in more detail .

* This filter is very useful for creating friendly URLs for SEO.

+1
source

Source: https://habr.com/ru/post/1342272/


All Articles