title: Docker: nginx reverse proxy and how to configure multiple services (i.e on 8080 &8081)
body: <p>I am a bit stuck with configuring multiple services where nginx is the proxy server.</p>
  
  <p>running : </p>
  
  <pre><code>docker -v                      
  Docker version 19.03.8, build afacb8b7f0
  
  docker-compose -v                              
  docker-compose version 1.23.2, build 1110ad01
  </code></pre>
  
  <p>I want to start with this test, everything in the same docker-compose.yml-file :
  link to <a href="https://hub.docker.com/r/jwilder/nginx-proxy/" rel="nofollow noreferrer">jwilder/nginx</a> </p>
  
  <ol>
  <li>proxy :  nginx-server (jwilder/nginx-proxy:0.7.0 , which is nginx 1.17.6) </li>
  <li>container1 : httpd:2.4</li>
  <li>container2 : httpd:2.4</li>
  </ol>
  
  <p>updating my /etc/hosts before I start <p></p>
  
  <ol>
  <li>127.0.0.1 container1.com</li>
  <li>127.0.0.1 container2.com</li>
  </ol>
  
  <p>Here is my <strong>docker-compose.yml-file</strong> (obs -> version 3.7)</p>
  
  <pre><code>version: '3.7'
  
  services:
  
    proxy:
      image: jwilder/nginx-proxy:0.7.0
      container_name: proxy-test
      ports:
        - "80:80"
        - "443:443"
      volumes:
        - /var/run/docker.sock:/tmp/docker.sock:ro
        - ./nginx-proxy.conf:/etc/nginx/conf.d/nginx-proxy.conf:ro
  
    container1:
      image: httpd:2.4
      container_name: container-1
      environment:
        - VIRTUAL_HOST:container1.com
      ports:
        - 8080:80
  
    container2:
      image: httpd:2.4
      container_name: container-2
      environment:
        - VIRTUAL_HOST:container2.com
      ports:
        - 8081:80
  </code></pre>
  
  <p>here is my <strong>nginx-proxy.conf</strong>:</p>
  
  <pre><code>server {
      listen          80;
      server_name     container1.com;
      location / {
          proxy_pass http://localhost:8080;
      }
  }
  
  server {
    listen        80;
    server_name   container2.com;
  
    location / {
      proxy_pass  http://localhost:8081;
    }
  }
  </code></pre>
  
  <p>After this I run the <p>
  <code>docker exec container-1 sed -i 's/It works!/Container 1/' /usr/local/apache2/htdocs/index.html</code> <strong>AND</strong> <p>  <code>docker exec container-2 sed -i 's/It works!/Container 2/' /usr/local/apache2/htdocs/index.html</code></p>
  
  <hr>
  
  <p>Test 1 :   with curl to the port 8080 and port 8081 </p>
  
  <pre><code>curl localhost:8080
  response -&gt; Container 1
  
  curl localhost:8081 
  response -&gt; Container 2
  </code></pre>
  
  <p>Test 2 : with curl to container1.com <strong>AND</strong> container2.com</p>
  
  <pre><code>curl container1.com
  status 502
  curl container2.com
  status 502
  </code></pre>
  
  <p>Are the settings in my conf wrong ?</p>
  
  <p>Troubleshooting 1:</p>
  
  <ol>
  <li><p><code>docker exec -it proxy-test bash</code> </p></li>
  <li><p>I can see that the nginx-proxy.conf is in the directory (/etc/nginx/conf.d)</p></li>
  <li><p>/etc/nginx/conf.d/default.conf is there as well</p></li>
  </ol>
  
  <p>Troubleshooting 2: The proxy-log (<code>Connection refused - while connecting to upstream</code>)</p>
  
  <pre><code>proxy-test    | nginx.1    | 2020/04/03 10:52:08 [error] 61#61: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 172.29.0.1, server: container1.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "container1.com"
  proxy-test    | nginx.1    | 2020/04/03 10:52:08 [error] 61#61: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 172.29.0.1, server: container1.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "container1.com"
  </code></pre>