161

I need to test sub-domains on my localhost. How can I effectively have this result of adding *.localhost.com to my /etc/hosts/ file?

If it's not possible, how do I work around this problem? I need to test wildcard sub-domains on my localserver. It is a Django devserver, can the Django dev server handle the sub-domains? Can some other piece of software/routing give me the end result I want?

migrated from stackoverflow.com Mar 2 '10 at 17:06

This question came from our site for professional and enthusiast programmers.

  • 1
    Belongs on superuser ? – Paul R Mar 2 '10 at 16:48
  • I know people say it's not possible! But how do I do it anyway! What is something outside of /etc/hosts/ I can use to get the effect. I'm testing a development server. – MikeN Mar 2 '10 at 20:17
  • As most answers are focusing on your first question (localhost subdomain wildcards), I'll answer your secondary question as a comment: yes, the Django dev server is perfectly capable of handling localhost subdomains, you just have to convince your browser and your OS to send the traffic its way (using one of the various solutions below)! – hheimbuerger Mar 9 '18 at 21:22

14 Answers 14

43

I have written a dns proxy in Python. It will read wildcard entries in /etc/hosts. See here: https://github.com/hubdotcom/marlon-tools/blob/master/tools/dnsproxy/dnsproxy.py

  • This is perfect! I have been looking for a simple solution like this for a long time (Working on OSX Mavericks BTW) – Billy Moon Nov 5 '13 at 22:07
  • 2
    now, if we could only just pip install it :) – metakermit Mar 25 '14 at 11:39
  • 1
    I installed this, did everything and ran it, but it does not block sites. – DisplayName Dec 13 '15 at 18:47
  • Can not upvote this enough. I've needed this forever, stumbled upon this gem of a question and answer, and I'm a bit of a Python hacker, and this is a neat tidbit. Thanks for sharing! – Farley Sep 8 '16 at 15:41
150

Install dnsmasq (I do this on all my Linux desktops as a DNS cache anyways). In dnsmasq.conf add the line:

address=/localhost.com/127.0.0.1
  • 7
    Brilliant! Note for Mac users, it really is this simple: 1. sudo port install dnsmasq 2. edit /opt/local/etc/dnsmasq.conf 3. sudo port load dnsmasq – tomc Nov 16 '11 at 11:57
  • 17
    OSX with brew: same as above but brew install dnsmasq – Matt Humphrey Mar 7 '13 at 21:41
  • 1
    fyi brew > port (re @MattHumphrey suggestion) – electblake Mar 22 '13 at 2:57
  • 1
    This example doesn't work for me, possibly because I'm using vagrantup.com for local serving. Don't worry, I already changed the IP to the correct one 192.168.50.11 but DNS is not resolving. Any ideas, is this example correct for "non-127.0.0.1" IP's? – Brian Oct 27 '13 at 19:55
  • 6
    Great tip. For an Ubuntu 14.04 desktop (which runs dnsmasq by default), create a file called /etc/NetworkManager/dnsmasq.d/dnsmasq-localhost.conf and put the line address=/localhost.com/127.0.0.1 in it, then restart. – user38397 May 21 '15 at 18:48
58

It is not possible to specify wildcards in the /etc/hosts file. Either specify the required hostnames explicitly or alternatively set up a local name server with the appropriate rules.

15

You need to set up a DNS server and have each client use it for resolution. The server itself can be something as "light" as dnsmasq or as heavy as BIND.

  • +1 for dnsmasq, which is really well-documented and easy to use – David Schmitt May 2 '11 at 12:24
  • dnsmasq is not able to do this. – DisplayName Dec 13 '15 at 18:51
  • What part of the behavior described in the --address= section of the dnsmasq.conf man page doesn't cover the use case describe above? – Gerald Combs Dec 14 '15 at 16:33
10

Simple Workflow (no need to install anything)

I personally like to create a PAC file for that and make my browser just use it.

Step 1: create a file e.g.: *.proxy.pac* somewhere (I use my $home folder)

Step 2: paste this code (example is with port 8000):

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*localhost")) {
    return "PROXY localhost:8000";
  }
  return "DIRECT";
}

Step 3: Make your Browser use this PAC file.

Youtube Video for PAC & Firefox

Step 4: Now you can test your app by accessing: http://mysubdomain.localhost/

Step 5: Enjoy :)

  • 2
    Perhaps it is worth mentioning that this will only affect your browser. Other programs (like wget, will not get affected by this). That is not per se a problem, but perhaps it is better to mention this. – Willem Van Onsem Apr 24 '18 at 21:02
  • If you go the .pac route and you're implementing a node.js server, you will be surprised to see that req.url is now an absolute URL. This is done because the assumption is that you want to write an actual proxy server, but it is surprising if you came to this technique just as a way to stop adding /etc/hosts entries for debugging. FYI to those who might go the same road I did. – Tom Boutell Jan 2 at 20:26
5

I've tidied up an old project of mine:

https://github.com/airtonix/avahi-aliases

requirements:

  • linux where avahi and python-avahi are installable
  • you're ok with .local domains (avahi doesn't support any other kind)

Advantages over using dnsmasq or the python dns proxy:

  • other avahi/bonjour users on your local network can resolve the aliases you create and announce to the network ( providing you're allowing access to port 5353 )
4

This DNS based solution worked perfectly in my case, without need to install anything : https://gist.github.com/fedir/04e60d679d5657d1f9f9aa10b3168282 (Mac OSX 10.9)

  • Such an amazing solution for Mac OSX. – iDev247 Sep 6 '12 at 7:25
  • Dead link, please update your link, your solution, or in some way deprecate this answer. Cheers. – Eric Hodonsky May 12 '17 at 17:07
  • Done (backup added) – Fedir RYKHTIK May 18 '17 at 12:50
3

You cannot use a wildcard in /etc/hosts.

Have a look here for a good walkthrough on how to accomplish on OS X using BIND, the built-in but inactive DNS server, and Apache.

  • 5
    Apache has nothing to do with subdomains. – Anonymous Mar 2 '10 at 17:55
3

If you want to use dnsmasq with NetworkManager you can (or even must?) start dnsmasq from NetworkManager by adding

dns=dnsmasq

to /etc/NetworkManager/NetworkManager.conf. Then the dnsmasq config goes to /etc/NetworkManager/dnsmasq.conf or /etc/NetworkManager/dnsmasq.d/ resp.

2

Short answer:

Your /etc/hosts/ file won't let you use wildcards or port numbers. You will need to create one entry for each of your subdomain

  • 2
    What would let you specify port numbers? Not DNS AFAIK. – ptman Mar 3 '10 at 11:45
2

The short answer is you don't. The longer answer is you need to be clearer on what you desire to actually achieve, because there is perhaps either a better way, and a different way to achieve it.

For web-hosting (I've never seen it used otherwise) is done in DNS in combination with a virtual hosting aware web server. For more information on wildcard DNS records (Wikipedia), and an article Wildcard hosting with Apache and Bind for Linux using bind and Apache.

At worst, you could use a local DNS server I suppose.

2

A common task for this subject is to map directories to subdomains. A very straightforward way for that is to append the directory-based entries automatically to the hosts file:

#!/usr/bin/python

import os

hostsFile = open("/etc/hosts", "a+");

lines = hostsFile.readlines()

for fileName in os.listdir('/opt/subdomainDirs'):

    entryExists = False
    for line in lines:
        if fileName in line:
            entryExists = True  

    if not entryExists:
        hostsFile.write("127.0.0.1 " + fileName + ".localhost\n");
2

Thank you tschundeee for what I consider to be the ultimate answer to this issue, wish I could just comment but here is the total configuration for those trying to accomplish the original goal (wildcards all pointing to same codebase -- install nothing, dev environment ie, XAMPP)

hosts file (add an entry)

file: /etc/hosts (non-windows)

127.0.0.1   example.local

httpd.conf configuration (enable vhosts)

file: /XAMPP/etc/httpd.conf

# Virtual hosts
Include etc/extra/httpd-vhosts.conf

httpd-vhosts.conf configuration

file: XAMPP/etc/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin admin@example.local
    DocumentRoot "/path_to_XAMPP/htdocs"
    ServerName example.local
    ServerAlias *.example.local
#    SetEnv APP_ENVIRONMENT development
#    ErrorLog "logs/example.local-error_log"
#    CustomLog "logs/example.local-access_log" common
</VirtualHost>

restart apache

create pac file:

save as whatever.pac wherever you want to and then load the file in the browser's network>proxy>auto_configuration settings (reload if you alter this)

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*example.local")) {
    return "PROXY example.local";
  }
  return "DIRECT";
}
2

dnsmasq worked for me, except I had to make some additional steps.

Here is the full procedure:

  1. Prepend /etc/resolv.conf with the following line

    nameserver 127.0.0.1
    
  2. Add the following lines to /etc/dnsmasq.conf

    listen-address=127.0.0.1
    address=/localhost.localdomain/127.0.0.1
    address=/localhost/127.0.0.1
    
  3. Restart dnsmasq

  • This will not work if your distro uses netplan (ubuntu 18.04 I'm looking at you) – MrMesees Feb 26 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.