Shahar 'Dawn' Or
Shahar 'Dawn' Or

Reputation: 2961

How to configure custom hostname to IP resolutions in my system for web development

Preface

A web app can potentially serve different pages, depending on the hostname that is requested by the browser, even if all hostnames are resolved to the same IP address.

Example

For example, at https://app.example.com, which resolves to 1.2.3.4, users will find the user interface and at https://admin.example.com, which also resolves to 1.2.3.4, awaits a dashboard through which only the app's owner can administrate users and data in the app.

What We Need

In short, we need to enter, for example, http://admin.app:8000/ in our browsers and have that admin.app resolve to 127.0.0.1.

The Question

How can I configure custom hostname to IP address resolutions in my development environment?

Upvotes: 1

Views: 4668

Answers (6)

god
god

Reputation: 299

(GNU/Linux)

Since all the major distributions are migrating (or already did) to systemd stack the proper place to implement wildcard support would be systemd-resolved: see https://github.com/systemd/systemd/issues/766 for details.

That would be the place to set custom overrides for DNS as well.

As for windows - its VM should just get DNS from host machine: it's to risky to run it on bare metal anyway.

Upvotes: 0

Shahar 'Dawn' Or
Shahar 'Dawn' Or

Reputation: 2961

(Windows, Linux, OSX) System Wide Hosts File

Simply edit the hosts file. Its location depends on the OS. For example:

127.0.0.1    app.localhost
127.0.0.1    admin.localhost

On Windows you can use this nifty open source GUI for editing the hosts file: Hosts File Editor.

Wildcards

The hosts file does not support wildcards!

Ubuntu Desktop

Since Ubuntu 12.04, Ubuntu desktop comes with a local DNS server, which might not respect the hosts file (/etc/hosts). So, for Ubuntu desktop, this answer is best.

Upvotes: 1

Shahar 'Dawn' Or
Shahar 'Dawn' Or

Reputation: 2961

(Windows) Configuring Acrylic DNS Server

Acrylic DNS Proxy is easy to install and configure.

It can help us get hostnames with aliases quickly in Windows.

And it is open source.

  1. Install it.
  2. Open the hosts file (via the start menu entry).
  3. Put in some entries, like 1.2.3.4 >app.
  4. Clear its cache and restart it (via the start menu entry).
  5. Set your DNS server to 127.0.0.1.

Upvotes: 0

Shahar 'Dawn' Or
Shahar 'Dawn' Or

Reputation: 2961

(Ubuntu and Derivatives) Configure NetworkManager's dnsmasq

Ubuntu Desktop's default networking configuration is composed of NetworkManager and its slave dnsmasq.

The slave dnsmasq listens at 127.0.1.1 and /etc/resolv.conf lists it as the only nameserver.

This has some benefits.

What it means for this purpose is that we have a fully configurable DNS server, comfortably configured by default.

We can create /etc/NetworkManager/dnsmasq.d/hosts.conf and put in it whatever address statements we'd like:

address=/admin.app/127.0.0.1

We can even use wildcards!

address=/.app/127.0.0.1

See the dnsmasq documentation for details (look for --address).

Since dnsmasq is started by the network-manager service, then I would assume that the following would restart it so that new configuration would take effect:

$ service network-manager restart

But its init-script does not control slave dnsmasq.

Therefore the dnsmasq process must be killed and then the above command would have it start again.

And that is it!

Upvotes: 1

Shahar 'Dawn' Or
Shahar 'Dawn' Or

Reputation: 2961

If Testing In a Virtual Machine

In a virtual machine 127.0.0.1 and localhost will not reach the host, but the guest.

In VirtualBox, for example, by default, the host can be reached at 10.0.2.2. So, the guest VM's hosts file can look like

10.0.2.2    host
10.0.2.2    app.host
10.0.2.2    admin.host

Proxy DNS Nameserver Inside a Virtual Machine

If you're setting up a proxy DNS nameserver inside a virtual machine (perhaps for wildcard support in Windows) the upstream nameserver is usually provided by the host. In VirtualBox, it is 10.0.2.3.

Upvotes: 0

Shahar 'Dawn' Or
Shahar 'Dawn' Or

Reputation: 2961

(Linux) User Specific HOSTALIASES File

Very limited

This would have been my preferred answer because it refrains from altering system configuration.

But:

What is It

It is a user specific host aliases file. Notice that the format is not the same as the hosts file.

In short, you create a file which contains host aliases.

For example

foo    localhost
bar    localhost

and place it at ~/.hosts.

Then you set an environment variable HOSTALIASES with the path to the aliases file. So, for this example

$ export HOSTALIASES=~/.hosts

Upvotes: 0

Related Questions