<p>I'm running Nginx on Kubernetes.</p>

<p>When I use the following proxy_pass directive it works as expected:</p>

<pre><code>proxy_pass "http://service-1.default";
</code></pre>

<p>However the following does not work:</p>

<pre><code>set $service "service-1";
proxy_pass "http://$service.default";
</code></pre>

<p>I get an error saying <code>no resolver defined to resolve service-1.default</code></p>

<p>As far as I can tell <code>proxy_pass</code> is receiving the exact same string so why is it behaving differently?</p>

<p>I need to use a variable because I'm dynamically getting the service name from the URL using a regex.</p>

<p>I've found the reason and a solution.</p>

<p>Nginx detects if a variable is being used in <code>proxy_pass</code> (I don't know how it does that). If there is no variable it resolved the hostname at startup and caches the IP address. If there is a variable it uses a resolver (DNS server) to lookup the IP at runtime.</p>

<p>So the solution is to specify the Kube DNS server like this:</p>

<pre><code>resolver kube-dns.kube-system.svc.cluster.local valid=5s;
set $service "service-1";
proxy_pass "http://$service.default.svc.cluster.local";
</code></pre>

<p>Note that the full local DNS name of the service must be used which you can get by running <code>nslookup service-1</code>.</p>
