title: nginx reverse proxy with Docker - Connection refused while connecting to upstream
body: <h1>Environment setup</h1>
  
  <p>I have an application which is composed by some services:</p>
  
  <ul>
  <li>jenkins server</li>
  <li>nginx server with angular</li>
  <li>nginx server as a proxy</li>
  </ul>
  
  <p>Those services are defined in the <code>docker-compose</code> file:</p>
  
  <pre><code>    version: '3'
  services:
    reverse:
      container_name: reverse-proxy
      build:
        context: /app/mywallet/MyWalletFe/reverse-proxy
      ports:
        - "80:80"
      networks:
        - net
    jenkins:
      container_name: jenkins
      image: jenkins/jenkins
      volumes:
        - "$PWD/jenkins_home:/var/jenkins_home"
        - "/var/run/docker.sock:/var/run/docker.sock"
      networks:
        - net
    angular:
      container_name: mywallet_fe
      build:
        context: /app/mywallet/MyWalletFe
      networks:
        - net
  networks:
    net:
  </code></pre>
  
  <p>I defined the following configuration file for the <code>reverse-proxy</code>:</p>
  
  <pre><code>upstream client {
    # angular is the name of the service in docker-compose file
    server angular:4200;
  }
  
  
  upstream jenkins {
    server jenkins:8080;
  }
  
  server {
    listen 80;
  
    location / {
      proxy_pass http://client;
    }
  
    location /jenkins {
      proxy_pass http://jenkins;
    }
  }
  </code></pre>
  
  <p>Finally, here is the <code>Dockerfile</code> for the <code>reverse-proxy</code> service, which copies the configuration file in the nginx container:</p>
  
  <pre><code>FROM nginx
  # override default files if present
  COPY ./default.conf /etc/nginx/conf.d/default.conf
  </code></pre>
  
  <h2>Goal</h2>
  
  <p>My goal is to access Jenkins with <code>SERVER_IP/jenkins</code></p>
  
  <h2>Output</h2>
  
  <p>When I run the whole application and try to access to <code>SERVER_IP/jenkins</code>, I get the following error in the reverse-proxy logs:</p>
  
  <blockquote>
    <p>2020/04/15 21:44:55 [error] 6#6: *10 connect() failed (111: Connection
    refused) while connecting to upstream, client: MY_CLIENT_IP, server: ,
    request: "GET /login?from=%2Fjenkins HTTP/1.1", upstream:
    "<a href="http://172.18.0.5:4200/login?from=%2Fjenkins" rel="nofollow noreferrer">http://172.18.0.5:4200/login?from=%2Fjenkins</a>", host: "SERVER_IP",
    referrer: "<a href="http://SERVER_IP/jenkins" rel="nofollow noreferrer">http://SERVER_IP/jenkins</a>" MY_CLIENT_IP <br>
    [15/Apr/2020:21:44:55 +0000] "GET /favicon.ico HTTP/1.1" 502 559
    "<a href="http://SERVER_IP/login?from=%2Fjenkins" rel="nofollow noreferrer">http://SERVER_IP/login?from=%2Fjenkins</a>" "Mozilla/5.0 (Windows NT
    10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36" "-" 2020/04/15 21:44:55 [error]
    6#6: *10 connect() failed (111: Connection refused) while connecting
    to upstream, client: MY_CLIENT_IP, server: , request: "GET
    /favicon.ico HTTP/1.1", upstream:
    "<a href="http://172.18.0.5:4200/favicon.ico" rel="nofollow noreferrer">http://172.18.0.5:4200/favicon.ico</a>", host: "SERVER_IP",
    referrer: "<a href="http://SERVER_IP/login?from=%2Fjenkins" rel="nofollow noreferrer">http://SERVER_IP/login?from=%2Fjenkins</a>"</p>
  </blockquote>
  
  <p>where <code>MY_CLIENT_IP</code> is my laptop IP and <code>SERVER_IP</code> is the IP of the server where the application runs.<br></p>
  
  <p>What's wrong in the configuration of the reverse proxy? If I expose the jenkins and angular services I can reach them, while through the proxy I can't.<br></p>
  
  <h3>Similar question, which doesn't help me (or I don't understand how would those help me)</h3>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/42475329/2993413">Docker nginx reverse proxy returns 502 bad gateway &quot;connection refused while connecting to upstream&quot;</a></li>
  <li><a href="https://stackoverflow.com/q/55052784/2993413">Connection refused while connection to upstream - Docker</a></li>
  <li><a href="https://stackoverflow.com/q/51122146/2993413">connect() failed (111: Connection refused) while connecting to upstream for nginx+php-fpm docker</a></li>
  <li><a href="https://stackoverflow.com/q/36923214/2993413">docker nginx connection refused while connecting to upstream</a></li>
  </ul>