Virtual Instance Testing on Localhost

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.

FWIW, CX are only supposed to affect the virtual instance they are deployed to.

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.

It fails for these commands because they don't do a full name resolution check, they are only talking to the configured DNS resolver. If the DNS resolver isn't configured to handle the resolution, it will fail.

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
These succeed because the browser and curl are both using full domain name resolution, so the *.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!

Blogs

The domain localhost is a reserved domain as per the standard (RFC 6761).

It is specifically meant to refer to the local machine (loopback address) and does not require DNS resolution.

By default, there are no DNS mechanisms to resolve *.localhost.

Right, so the browser sends all traffic to localhost, but Liferay will respect the hostname in the URL with respect to routing of the virtual host.

So we're taking advantage of the fact that Liferay isn't necessarily resolving the domain, but it will use it to determine the virtual host to return content from.