title: Docker + Nginx: Getting proxy_pass to work
body: <p>I'm having a problem trying to get Nginx to proxy a path to another server that is also running in Docker.</p>
  
  <p>To illustrate, I'm using Nexus server as an example.</p>
  
  <p>This is my first attempt...</p>
  
  <p><code>docker-compose.yml</code>:-</p>
  
  <pre><code>version: '2'
  services:
    nexus:
      image: "sonatype/nexus3"
      ports:
       - "8081:8081"
      volumes:
       - ./nexus:/nexus-data
  
    nginx:
      image: "nginx"
      ports:
      - "80:80"
      volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
  </code></pre>
  
  <p><code>nginx.conf</code>:-</p>
  
  <pre><code>worker_processes 4;
  events { worker_connections 1024; }
  http {
          server {
                listen 80;
  
                location /nexus/ {
                  proxy_pass http://localhost:8081/;
                }
          }
  }
  </code></pre>
  
  <p>When I hit <code>http://localhost/nexus/</code>, I get 502 Bad Gateway with the following log:-</p>
  
  <pre><code>nginx_1  | 2017/05/29 02:20:50 [error] 7#7: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /nexus/ HTTP/1.1", upstream: "http://[::1]:8081/", host: "localhost"
  nginx_1  | 2017/05/29 02:20:50 [error] 7#7: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /nexus/ HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "localhost"
  nginx_1  | 172.18.0.1 - - [29/May/2017:02:20:50 +0000] "GET /nexus/ HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
  </code></pre>
  
  <p>In my second attempt...,</p>
  
  <p><code>docker-compose.yml</code> -  I added <code>links</code> to Nginx configuration:-</p>
  
  <pre><code>version: '2'
  services:
    nexus:
      image: "sonatype/nexus3"
      ports:
       - "8081:8081"
      volumes:
       - ./nexus:/nexus-data
  
    nginx:
      image: "nginx"
      ports:
      - "80:80"
      volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      links:
      - nexus:nexus
  </code></pre>
  
  <p><code>nginx.conf</code>... Instead of using <code>http://localhost:8081/</code>, I use <code>http://nexus:8081/</code>:-</p>
  
  <pre><code>worker_processes 4;
  events { worker_connections 1024; }
  http {
          server {
                listen 80;
  
                location /nexus/ {
                  proxy_pass http://nexus:8081/;
                }
          }
  }   
  </code></pre>
  
  <p>Now, when I hit <code>http://localhost/nexus/</code>, it gets proxied properly but the web content is partially rendered. When inspecting the HTML source code of that page, the javascript, stylesheet and image links are pointing to <code>http://nexus:8081/[path]</code>... hence, 404.</p>
  
  <p>What should I change to get this to work properly?</p>
  
  <p>Thank you very much.</p>