Search code examples
javamacosspring-boottomcatwildcard-subdomain

Wildcard Subdomain In SpringBoot For my Mac


I am creating a web application in which there will be multiple subdomain. Each client will be having its own subdomain. Each subdomain will have individual database. For example I am having a client abc, then it will have subdomain as abc.mydomain.com and its database name will be abc.

Now this application is created in spring boot. Here I want to configure wildcard subdomain. So how can I configure it in spring boot. For this, I had tried few solution.

I had changed my /etc/hosts and added an domain name entry in it as below

127.0.0.1 www.mydomain.com

But now I want subdomain in this. How can I do it.

I had seen somewhere that I need to configure server.xml of tomcat. In server.xml I had added below line.

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

Can you please help me with this. Also let me know incase you need more details in this.


Solution

  • After been working for multiple days I came to a solution for this question. As I was working for wildcard subdomain in localhost. Thus for this I need to use an external software called dnsmasq, I had executed following commands to install dnsmasq in my laptop.

    brew install dnsmasq

    After installing dnsmasq in my laptop I need to to configure it. For this I have to update the dnsmasq. For my case it is located at

    /usr/local/etc/dnsmasq.conf

    In this file I have to add few lines to configure wildcard subdomain. Lines are as below.

    # Route all *.mydomain.com addresses to localhost
    address=/mydomain.com/127.0.0.1
    # Don't read /etc/resolv.conf or any other configuration files.
    no-resolv
    # Never forward plain names (without a dot or domain part)
    domain-needed
    # Never forward addresses in the non-routed address spaces.
    bogus-priv
    

    Next I need to restart my dnsmasq service. Command for it is as below.

    sudo brew services start dnsmasq

    Finally you need to create a file mydomain.com at /etc/resolver (Create folder resolver if not exists). Add below line in mydomain.com file.

    nameserver 127.0.0.1

    This is the complete configuration for setting up swildcard subdomain in your mac os.

    For reference you can follow this link