Answers: <p>You will notice in this upstream block:</p>
  
  <pre><code>upstream api {
      server server:5000;
  }
  </code></pre>
  
  <p>I called it <code>api</code> but then I say <code>server server:5000;</code>. That was the problem because I originally had it as:</p>
  
  <pre><code>upstream server {
      server server:5000;
  }
  </code></pre>
  
  <p>but then thought that <code>server</code> might be a reserved operator in the Nginx world so I changed it to <code>api</code> but did not change it inside the block.</p>
  
  <p>So it should have been:</p>
  
  <pre><code>upstream api {
      server api:5000;
  }
  </code></pre>
  
  <p>After changing it, the xhr pending and timing out 502 errors went away.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-03-11T23:44:36.910Z
Answers: 2019-03-11T23:44:36.910Z
Answers: Daniel
Body: <p>I have a React application where a user can enter in some number, there is an algorithm that runs some calculation and spits out a result, but before we get there, Nginx is looking at the request and deciding whether to route it to React or Express server.</p>
  
  <p>Well when the number is submitted, there is an xhr request that remains pending until it just times out with a 502 bad gateway error.</p>
  
  <p>All this is happening inside of a Docker container.</p>
  
  <p>I ran a <code>docker logs &lt;container-id&gt;</code> and kept seeing this:</p>
  
  <pre><code>upstream server temporarily disabled while connecting to upstream
  </code></pre>
  
  <p>I thought it might be a typo in my Express server or in my worker process or even in the React application but I find none, so I am left wondering if the issue has to do with how I built my <code>default.conf</code> file:</p>
  
  <pre><code>upstream client {
      server client:3000;
  }
  
  upstream api {
      server server:5000;
  }
  
  server {
      listen 80;
  
      location / {
          proxy_pass http://client;
      }
  
      location /sockjs-node {
          proxy_pass http://client;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "Upgrade";
      }
  
      location /api {
          rewrite /api/(.*) /$1 break;
          proxy_pass http://api;
      }
  }
  </code></pre>
  
  <p>I feel like my knowledge of Nginx configurations and upstream is limited to where something may not be correct about this setup. After running for awhile the whole application crashed.</p>
  
  <p>To get this configuration to override the default <code>default.conf</code> inside of Nginx I setup a <code>Dockerfile.dev</code> inside of <code>nginx/</code> folder:</p>
  
  <pre><code>FROM nginx
  COPY ./default.conf /etc/nginx/conf.d/default.conf
  </code></pre>
  
  <p>The application overall is using this <code>docker-compose.yml</code> file:</p>
  
  <pre><code>version: "3"
  services:
    postgres:
      image: "postgres:latest"
    redis:
      image: "redis:latest"
    nginx:
      restart: always
      build:
        dockerfile: Dockerfile.dev
        context: ./nginx
      ports:
        - "3050:80"
    api:
      build:
        dockerfile: Dockerfile.dev
        context: ./server
      volumes:
        - /app/node_modules
        - ./server:/app
      environment:
        - REDIS_HOST=redis
        - REDIS_PORT=6379
        - PGUSER=postgres
        - PGHOST=postgres
        - PGDATABASE=postgres
        - PGPASSWORD=postgres_password
        - PGPORT=5432
    client:
      build:
        dockerfile: Dockerfile.dev
        context: ./client
      volumes:
        - /app/node_modules
        - ./client:/app
    worker:
      build:
        dockerfile: Dockerfile.dev
        context: ./worker
      volumes:
        - /app/node_modules
        - ./worker:/app
  </code></pre>
  
  <p>I am also wondering if the <code>default.conf</code> file is fine, but its my Express API that is taking too long to respond.</p>
  
  <p>Then towards the bottom of the <code>docker-compose.yml</code> file I added some Redis environment variables I thought it may have needed below <code>worker</code>:</p>
  
  <pre><code> worker:
      build:
        dockerfile: Dockerfile.dev
        context: ./worker
      volumes:
        - /app/node_modules
        - ./worker:/app
      environment:
        - REDIS_HOST=redis
        - REDIS_PORT=6379
  </code></pre>
  
  <p>I rebuilt the image with <code>docker-compose up --build</code> and restarted the application, and I can see the previous xhr requests still pending and they still give me a 502 and 504 error after a non-zero amount of time pending.</p>
  
Tags: <docker><docker-compose><nginx>
Title: Nginx routing setup within Docker container, request timing out with 502
UserDisplayName: Daniel
ThreadText: # Nginx routing setup within Docker container, request timing out with 502
  
  (Asked by: username_0 on 2019-03-11)
  
  I have a React application where a user can enter in some number, there is an algorithm that runs some calculation and spits out a result, but before we get there, Nginx is looking at the request and deciding whether to route it to React or Express server.
  
  Well when the number is submitted, there is an xhr request that remains pending until it just times out with a 502 bad gateway error.
  
  All this is happening inside of a Docker container.
  
  I ran a `docker logs <container-id>` and kept seeing this:
  
  ```
  upstream server temporarily disabled while connecting to upstream
  
  ```
  
  I thought it might be a typo in my Express server or in my worker process or even in the React application but I find none, so I am left wondering if the issue has to do with how I built my `default.conf` file:
  
  ```
  upstream client {
      server client:3000;
  }
  
  upstream api {
      server server:5000;
  }
  
  server {
      listen 80;
  
      location / {
          proxy_pass http://client;
      }
  
      location /sockjs-node {
          proxy_pass http://client;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "Upgrade";
      }
  
      location /api {
          rewrite /api/(.*) /$1 break;
          proxy_pass http://api;
      }
  }
  
  ```
  
  I feel like my knowledge of Nginx configurations and upstream is limited to where something may not be correct about this setup. After running for awhile the whole application crashed.
  
  To get this configuration to override the default `default.conf` inside of Nginx I setup a `Dockerfile.dev` inside of `nginx/` folder:
  
  ```
  FROM nginx
  COPY ./default.conf /etc/nginx/conf.d/default.conf
  
  ```
  
  The application overall is using this `docker-compose.yml` file:
  
  ```
  version: "3"
  services:
    postgres:
      image: "postgres:latest"
    redis:
      image: "redis:latest"
    nginx:
      restart: always
      build:
        dockerfile: Dockerfile.dev
        context: ./nginx
      ports:
        - "3050:80"
    api:
      build:
        dockerfile: Dockerfile.dev
        context: ./server
      volumes:
        - /app/node_modules
        - ./server:/app
      environment:
        - REDIS_HOST=redis
        - REDIS_PORT=6379
        - PGUSER=postgres
        - PGHOST=postgres
        - PGDATABASE=postgres
        - PGPASSWORD=postgres_password
        - PGPORT=5432
    client:
      build:
        dockerfile: Dockerfile.dev
        context: ./client
      volumes:
        - /app/node_modules
        - ./client:/app
    worker:
      build:
        dockerfile: Dockerfile.dev
        context: ./worker
      volumes:
        - /app/node_modules
        - ./worker:/app
  
  ```
  
  I am also wondering if the `default.conf` file is fine, but its my Express API that is taking too long to respond.
  
  Then towards the bottom of the `docker-compose.yml` file I added some Redis environment variables I thought it may have needed below `worker`:
  
  ```
   worker:
      build:
        dockerfile: Dockerfile.dev
        context: ./worker
      volumes:
        - /app/node_modules
        - ./worker:/app
      environment:
        - REDIS_HOST=redis
        - REDIS_PORT=6379
  
  ```
  
  I rebuilt the image with `docker-compose up --build` and restarted the application, and I can see the previous xhr requests still pending and they still give me a 502 and 504 error after a non-zero amount of time pending.
  
  **Tags:** docker, docker-compose, nginx
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-03-11. Score: 1 | Accepted answer
  
  You will notice in this upstream block:
  
  ```
  upstream api {
      server server:5000;
  }
  
  ```
  
  I called it `api` but then I say `server server:5000;`. That was the problem because I originally had it as:
  
  ```
  upstream server {
      server server:5000;
  }
  
  ```
  
  but then thought that `server` might be a reserved operator in the Nginx world so I changed it to `api` but did not change it inside the block.
  
  So it should have been:
  
  ```
  upstream api {
      server api:5000;
  }
  
  ```
  
  After changing it, the xhr pending and timing out 502 errors went away.
  
  
  ---
  
  