James
James

Reputation: 18379

Tomcat forward all subdomains to virtual host

I have a single tomcat server that have 4 websites on it.

I use virtual hosts to allow this. But now I need to forward "all" subdomains to a single host.

i.e. in my serveral.xml I want,

  <Host name="www.acme2.com"  appBase="acme2"
        unpackWARs="true" autoDeploy="false">
    <Alias>acme2.com</Alias>
    <Alias>*.acme2.com</Alias>
  </Host>

I know I can add subdomains one by one, but I want "all" subdomains to use the same application. My DNS forwards all subdomains to the server IP.

This works for my default host, as all subdomains get sent to it, but I want each virtual hosts subdomains sent to its own app.

Upvotes: 3

Views: 2086

Answers (2)

SkyWalker
SkyWalker

Reputation: 29130

From VirtualHost Examples

Running several name-based web sites on a single IP address.

Your server has a single IP address, and multiple aliases (CNAMES) point to this machine in DNS. You want to run a web server for www.example1.com and www.example2.org on this machine.

Note

Creating virtual host configurations on your Apache server does not magically cause DNS entries to be created for those host names. You must have the names in DNS, resolving to your IP address, or nobody else will be able to see your web site. You can put entries in your hosts file for local testing, but that will work only from the machine with those hosts entries.

Server configuration

# Ensure that Apache listens on port 80

    Listen 80

# Listen for virtual host requests on all IP addresses

    NameVirtualHost *:80

    <VirtualHost *:80>
    DocumentRoot /www/example1
    ServerName www.example1.com

# Other directives here

    </VirtualHost>

    <VirtualHost *:80>
    DocumentRoot /www/example2
    ServerName www.example2.org

# Other directives here

    </VirtualHost>

The asterisks match all addresses, so the main server serves no requests. Due to the fact that www.example1.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first VirtualHost.

Note

You can, if you wish, replace * with the actual IP address of the system. In that case, the argument to VirtualHost must match the argument to

NameVirtualHost:

NameVirtualHost 172.20.30.40

<VirtualHost 172.20.30.40>
# etc ...

However, it is additionally useful to use * on systems where the IP address is not predictable - for example if you have a dynamic IP address with your ISP, and you are using some variety of dynamic DNS solution. Since * matches any IP address, this configuration would work without changes whenever your IP address changes.

The above configuration is what you will want to use in almost all name-based virtual hosting situations. The only thing that this configuration will not work for, in fact, is when you are serving different content based on differing IP addresses or ports.

Resource Link:

  1. url redirection/mapping of the same java web application to multiple sub-domains
  2. Two domain & their sub-domain are mapped to the same IP so how can i host my different application to the same tomcat server?
  3. How to host random or wildcard subdomains on Tomcat and Java ?
  4. Multiple Subdomains in Apache / Tomcat with mod_jk

Upvotes: 1

Tiz
Tiz

Reputation: 707

Use nginx to configure the redirects. In the domain config file you can specify

server {
  server_name *.domain.com;
  return 301 http://domain.com$request_uri;
}

To redirect from sub-domains to the domain.

The nginx website has quite a good Getting Started guide.

Upvotes: 0

Related Questions