<p>Not sure if this is OS specific, but on my M1 Mac, I'm installing the Nginx controller and resource example located in the official <a href="https://kubernetes.github.io/ingress-nginx/deploy/#quick-start" rel="nofollow noreferrer">Quick Start guide for the controller.</a> for Docker Desktop for Mac. The instructions are as follows:</p>
<pre><code>// Create the Ingress
helm upgrade --install ingress-nginx ingress-nginx \
  --repo https://kubernetes.github.io/ingress-nginx \
  --namespace ingress-nginx --create-namespace

// Pre-flight checks
kubectl get pods --namespace=ingress-nginx

kubectl wait --namespace ingress-nginx \
  --for=condition=ready pod \
  --selector=app.kubernetes.io/component=controller \
  --timeout=120s

// and finally, deploy and test the resource.
kubectl create deployment demo --image=httpd --port=80
kubectl expose deployment demo

kubectl create ingress demo-localhost --class=nginx \
  --rule=demo.localdev.me/*=demo:80

kubectl port-forward --namespace=ingress-nginx service/ingress-nginx-controller 8080:80
</code></pre>
<p>I noticed that the instructions did not mention having to edit the <code>/etc/hosts</code> file, which I found strange. And, when I tested it by putting <code>demo.localdev.me:8080</code> into the browser, it did work as expected!</p>
<p>But why? What happened that an application inside of a docker container was able to influence behavior on my host machine and intercept its web traffic without me having to edit the <code>/etc/hosts</code> file?</p>
<p>For my next test, I re-executed everything above with the only change being that I switched <code>demo</code> to <code>demo2</code>. That did <strong>not</strong> work. I did have to go into <code>/etc/hosts</code> and add <code>demo2.localdev.me 127.0.0.1</code> as an entry. After that both demo and demo2 work as expected.</p>
<p>Why is this happening? Not having to edit the /etc/hosts file is appealing. Is there a way to configure it so that they all work? How would I turn it &quot;off&quot; from happening automatically if I needed to route traffic back out to the internet rather than my local machine?</p>

<p>I replicated your issue and got a similar behaviour on the Ubuntu 20.04.3 OS.</p>
<p>The problem is that <a href="https://kubernetes.github.io/ingress-nginx/deploy/#local-testing" rel="noreferrer">NGINX Ingress controller Local testing guide</a> did not mention that <a href="https://mxtoolbox.com/SuperTool.aspx?action=a%3ademo.localdev.me&amp;run=toolpage" rel="noreferrer"><code>demo.localdev.me</code> address points to <code>127.0.0.1</code></a> - that's why it works without editing <code>/etc/hosts</code> or <code>/etc/resolve.conf</code> file. Probably it's something like <a href="https://readme.localtest.me/" rel="noreferrer"><code>*.localtest.me</code> addresses</a>:</p>
<blockquote>
<p>Here’s how it works. The entire domain name localtest.me—and all wildcard entries—point to 127.0.0.1. So without any changes to your host file you can immediate start testing with a local URL.</p>
</blockquote>
<p>Also good and detailed explanation in <a href="https://superuser.com/questions/1280827/why-does-the-registered-domain-name-localtest-me-resolve-to-127-0-0-1">this topic</a>.</p>
<p>So Docker Desktop / Kubernetes change nothing on your host.</p>
<p>The <a href="https://mxtoolbox.com/SuperTool.aspx?action=a%3ademo2.localdev.me&amp;run=toolpage" rel="noreferrer">address <code>demo2.localdev.me</code> also points to <code>127.0.0.1</code></a>, so it should work as well for you - and as I tested in my environment the behaviour was exactly the same as for the <code>demo.localdev.me</code>.</p>
<p>You may run <a href="https://www.oreilly.com/library/view/mac-os-x/0596003706/re315.html" rel="noreferrer"><code>nslookup</code> command</a> and check which IP address is pointed to the specific domain name, for example:</p>
<pre><code>user@shell:~$ nslookup demo2.localdev.me
Server:         127.0.0.53
Address:        127.0.0.53#53

Non-authoritative answer:
Name:   demo2.localdev.me
Address: 127.0.0.1
</code></pre>
<p>You may try to do some tests with other hosts name, like some existing ones or no-existing then of course it won't work because the address won't be resolved to the <code>127.0.0.1</code> thus it won't be forwarded to the Ingress NGINX controller. In these cases, you can edit <code>/etc/hosts</code> (as you did) or use <a href="https://riptutorial.com/curl/example/31719/change-the--host---header" rel="noreferrer"><code>curl</code> flag <code>-H</code></a>, for example:</p>
<p>I created the ingress using following command:</p>
<pre><code>kubectl create ingress demo-localhost --class=nginx --rule=facebook.com/*=demo:80
</code></pre>
<p>Then I started port-forwarding and I run:</p>
<pre><code>user@shell:~$ curl -H &quot;Host: facebook.com&quot; localhost:8080
&lt;html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;
</code></pre>
<p>You wrote:</p>
<blockquote>
<p>For my next test, I re-executed everything above with the only change being that I switched <code>demo</code> to <code>demo2</code>. That did <strong>not</strong> work. I did have to go into <code>/etc/hosts</code> and add <code>demo2.localdev.me 127.0.0.1</code> as an entry. After that both demo and demo2 work as expected.</p>
</blockquote>
<p>Well, that sounds strange, could you run <code>nslookup demo2.localdev.me</code> without adding an entry in the <code>/etc/hosts</code> and then check? Are you sure you performed the correct query before, did you not change something on the Kubernetes configuration side? As I tested (and presented above), it should work exactly the same as for <code>demo.localdev.me</code>.</p>
