title: fast api and nginx in docker-compose shows connect() failed (111: Connection refused) while connecting to upstream, client: 172.27.0.1, server:
body: <h1>fastapi nginx template</h1>
  <p><a href="https://github.com/ryosukemitake/fastapi-nginx-template" rel="nofollow noreferrer">source code</a></p>
  <p>it's fast api and nginx template but currently it does not work as i expected.
  when you curl localhost, it responses 502 bad gateway instaed of hellow world which is defined in main.py ...</p>
  <p>docker logs web shows <code>connect() failed (111: Connection refused) while connecting to upstream, client: 172.27.0.1, server:</code> in nginx container.</p>
  <p>but why ?? how can you fix this error ?</p>
  <h2>file tree</h2>
  <pre><code>.
  ├── Dockerfile
  ├── README.md
  ├── docker-compose.yml
  ├── main.py
  ├── nginx
  │   ├── Dockerfile
  │   └── nginx.conf
  └── requirements.txt
  </code></pre>
  <h2>build and start</h2>
  <ol>
  <li><p><code>docker-compose up -d --build</code></p>
  </li>
  <li><p><code>docker logs ps -a</code></p>
  </li>
  </ol>
  <pre><code>CONTAINER ID   IMAGE                         COMMAND                  CREATED         STATUS         PORTS                                       NAMES
  cb79d9efaf75   fast-api-nginx-template_web   &quot;/docker-entrypoint.…&quot;   4 minutes ago   Up 4 minutes   0.0.0.0:80-&gt;80/tcp, :::80-&gt;80/tcp           web
  6b049c395508   fast-api-nginx-template_api   &quot;uvicorn main:app --…&quot;   4 minutes ago   Up 4 minutes   0.0.0.0:8000-&gt;8000/tcp, :::8000-&gt;8000/tcp   api
  </code></pre>
  <ol start="3">
  <li><code>curl localhost</code></li>
  </ol>
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;title&gt;502 Bad Gateway&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
  &lt;center&gt;&lt;h1&gt;502 Bad Gateway&lt;/h1&gt;&lt;/center&gt;
  &lt;hr&gt;&lt;center&gt;nginx/1.21.1&lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <ol start="4">
  <li><code>docker logs web</code></li>
  </ol>
  <pre><code>/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
  /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
  /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
  10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
  10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
  /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
  /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
  /docker-entrypoint.sh: Configuration complete; ready for start up
  2021/08/28 13:58:37 [error] 31#31: *8 connect() failed (111: Connection refused) while connecting to upstream, client: 172.27.0.1, server: 127.0.0.1, request: &quot;GET / HTTP/1.1&quot;, upstream: &quot;http://172.27.0.3:8000/&quot;, host: &quot;localhost&quot;
  172.27.0.1 - - [28/Aug/2021:13:58:37 +0000] &quot;GET / HTTP/1.1&quot; 502 157 &quot;-&quot; &quot;curl/7.64.1&quot;
  </code></pre>
  <ol start="5">
  <li><code>docker logs api</code></li>
  </ol>
  <pre><code>INFO:     Started server process [1]
  INFO:     Waiting for application startup.
  INFO:     Application startup complete.
  INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
  </code></pre>
  <h2>docker-compose</h2>
  <p>docker-compose</p>
  <pre><code>version: &quot;3.9&quot;
  services:
      web:
          container_name: web
          build: nginx
          ports:
            - 80:80
          depends_on:
            - api
          networks:
            - local-net
      api:
          container_name: api
          build: .
          ports:
            - 8000:8000
          networks:
            - local-net
          expose:
            - 8000
  networks:
    local-net:
      driver: bridge
  </code></pre>
  <h2>web</h2>
  <p>nginx/Dockerfile</p>
  <pre><code>FROM nginx
  RUN apt-get update 
  COPY nginx.conf /etc/nginx/nginx.conf
  </code></pre>
  <p>nginx/nginx.conf</p>
  <pre><code>worker_processes 1;
  
  events {
    worker_connections 1024; 
    accept_mutex off; 
    use epoll;
  }
  
  http {
      include mime.types;
      upstream app_serve {
          server web:8000;
      }
      access_log /var/log/nginx/access.log;
      error_log /var/log/nginx/error.log;
      server {
          listen 80 ipv6only=on;
          server_name 127.0.0.1;   
          location / {
              proxy_pass http://app_serve; 
              proxy_set_header Host               $host;
              proxy_set_header X-Real-IP          $remote_addr;
              proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
              proxy_set_header X-Forwarded-Proto  $scheme;
          }
      }
  }
  </code></pre>
  <h2>api</h2>
  <p>Dockerfile</p>
  <pre><code>ARG BASE_IMAGE=python:3.8-buster                                                                                
  FROM $BASE_IMAGE                                                                                                     
  
  RUN apt-get -y update &amp;&amp; \                                                                                           
      apt-get install -y --no-install-recommends \                                                                     
      build-essential \                                                                                                
      openssl libssl-dev \                                                                                             
      &amp;&amp; apt-get clean \                                                                                               
      &amp;&amp; rm -rf /var/lib/apt/lists/*                                                                                   
  
  ARG USER_NAME=app
  ARG USER_UID=1000
  ARG PASSWD=password
  
  RUN useradd -m -s /bin/bash -u $USER_UID $USER_NAME &amp;&amp; \
      gpasswd -a $USER_NAME sudo &amp;&amp; \
      echo &quot;${USER_NAME}:${PASSWD}&quot; | chpasswd &amp;&amp; \
      echo &quot;${USER_NAME} ALL=(ALL) ALL&quot; &gt;&gt; /etc/sudoers
  
  COPY ./ /app
  RUN chown -R ${USER_NAME}:${USER_NAME} /app
  
  USER $USER_NAME
  WORKDIR /app
  ENV PATH $PATH:/home/${USER_NAME}/.local/bin
  
  RUN pip3 install --user --upgrade pip 
  RUN pip3 install --user -r requirements.txt
  RUN rm -rf ~/.cache/pip/* 
  
  EXPOSE 8000
  
  # Execute
  CMD [&quot;uvicorn&quot;, &quot;main:app&quot;, &quot;--host&quot;, &quot;127.0.0.1&quot; ,&quot;--port&quot; ,&quot;8000&quot;]
  </code></pre>
  <p>main.py</p>
  <pre><code>from typing import Optional
  from fastapi import FastAPI
  
  app = FastAPI()
  
  @app.get(&quot;/&quot;)
  def load_root():
      return {&quot;Hello&quot;: &quot;World&quot;}
  
  @app.get(&quot;/items/{item_id}&quot;)
  def load_item(item_id: int, q: Optional[str] = None):
      return {&quot;item_id&quot;: item_id, &quot;q&quot;: q}
  
  </code></pre>