Blogs
I wanted to test a Liferay Multitenancy setup with virtual instances on my local system, but how to do it without faking out dns? Read on to figure out how to try it...

Virtual Instance Testing on Localhost
Just a quick one today...
So I'm working on some Client Extensions and wanted to see the impact that deployment would have across virtual instances.
But, normally to access different virtual instances, you need different virtual hosts (domain names) in order to access the different instances.
But, when you're running locally, and maybe when you don't have access/permissions to write your /etc/hosts file, you might be wondering how you can access the other instances...
As it turns out, there is this funny rule in domain name
resolution which effectively makes *.localhost
resolve
to 127.0.0.1
/::1
.
Now, this often won't be immediately clear. I mean, I dropped down to the command line to try some commands:
$ nslookup example.localhost Server: fe80::9691:7fff:fe47:ade1%21 Address: fe80::9691:7fff:fe47:ade1%21#53 ** server can't find example.localhost: NXDOMAIN $ ping example.localhost ping: cannot resolve example.localhost: Unknown host $ dig example.localhost ; <<>> DiG 9.10.6 <<>> example.localhost ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 49884 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;example.localhost. IN A ;; Query time: 0 msec ;; SERVER: fe80::9691:7fff:fe47:ade1%21#53(fe80::9691:7fff:fe47:ade1%21) ;; WHEN: Mon Nov 25 15:00:19 EST 2024 ;; MSG SIZE rcvd: 46
Now this of course will lead you to believe this won't really work as you hoped, but trust me, it does.
When you try to access
http://example.localhost:8080
in your browser, it will
work. Likewise you can use the command:
$ curl -o /dev/null -s -w "%{http_code}\n" http://example.localhost:8080 200
*.localhost
rule will be correctly applied.So, on your local system, as long as you designate your virtual
hosts using the *.localhost
format, you'll be able to
access the different virtual instances that you create on your system
without changing the /etc/hosts file or really making any system
changes at all.
Enjoy!