title: Dynamic proxy_pass in nginx to another pod in Kubernetes
body: <p>I'm trying to create an nginx proxy that forwards requests to <code>/&lt;service&gt;</code> to <code>http://&lt;service&gt;</code>. I first tried the following:</p>
  
  <pre><code>location ~ ^/(.+)$ {
      set $backend "http://$1:80";
      proxy_pass $backend;
  }
  </code></pre>
  
  <p>But it fails saying something like (when calling <code>/myservice</code>):</p>
  
  <pre><code>[error] 7741#0: *1 no resolver defined to resolve http://myservice
  </code></pre>
  
  <p>Since <code>myservice</code> is not externally accessible I've tried to install <a href="https://github.com/janeczku/go-dnsmasq" rel="noreferrer">go-dnsmasq</a> as a sidecar in the same pod and I try to use it for DNS resolution (like I've seen in <a href="https://www.jethrocarr.com/2013/11/02/nginx-reverse-proxies-and-dns-resolution/" rel="noreferrer">this</a> example) and change my nginx config to look like this:</p>
  
  <pre><code>location ~ ^/(.+)$ {
          resolver 127.0.0.1:53;
          set $backend "http://$1:80";
          proxy_pass $backend;
  }
  </code></pre>
  
  <p>But now nginx fails with:</p>
  
  <pre><code>[error] 9#9: *734 myservice could not be resolved (2: Server failure), client: 127.0.0.1, server: nginx-proxy, request: "GET /myservice HTTP/1.1", host: "localhost:8080"
  127.0.0.1 - xxx [30/May/2016:10:34:23 +0000] "GET /myservice HTTP/1.1" 502 173 "-" "curl/7.38.0" "-"
  </code></pre>
  
  <p>My Kubernetes pod looks like this:</p>
  
  <pre><code>spec:
    containers:
      - name: nginx
        image: "nginx:1.10.0"
        ports:
          - containerPort: 8080
            name: "external"
            protocol: "TCP"
      - name: dnsmasq
        image: "janeczku/go-dnsmasq:release-1.0.5"
        args:
          - --listen
          - "0.0.0.0:53"
  </code></pre>
  
  <p>Running <code>netstat -ntlp</code> in the dnsmasq container gives me:</p>
  
  <pre><code>Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
  tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      -
  tcp        0      0 :::53                   :::*                    LISTEN      1/go-dnsmasq
  </code></pre>
  
  <p>And running <code>nmap --min-parallelism 100 -sT -sU localhost</code> in the nginx container: </p>
  
  <pre><code>Starting Nmap 6.47 ( http://nmap.org ) at 2016-05-30 10:33 UTC
  Nmap scan report for localhost (127.0.0.1)
  Host is up (0.00055s latency).
  Other addresses for localhost (not scanned): 127.0.0.1
  Not shown: 1997 closed ports
  PORT     STATE SERVICE
  53/tcp   open  domain
  8080/tcp open  http-proxy
  53/udp   open  domain
  </code></pre>
  
  <p>So it seems that dnsmasq and nginx are indeed up and running? What could I be doing wrong?</p>