Sling Academy
Home/DevOps/NGINX & Docker error: Host not found in upstream

NGINX & Docker error: Host not found in upstream

Last updated: January 20, 2024

Understanding the NGINX Docker Upstream Error

When using NGINX as a reverse proxy in a Docker environment, you might occasionally encounter the error message ‘Host not found in upstream’. This error signifies that NGINX is unable to resolve the service name provided in the upstream configuration to a network address.

Common Causes

  • Service-specific network not being accessible to the NGINX container.
  • Typos or misconfiguration in the NGINX configuration file.
  • DNS resolution issues within the Docker network.
  • Timing problems where the dependent service may not be up and running before NGINX starts.

Possible Solutions to Fix the Error

Solution 1: Correct Service Names

Ensure all referenced service names in your NGINX configuration match exactly with those defined in your docker-compose.yml or Docker service commands.

Steps:

  1. Open the NGINX configuration file (nginx.conf).
  2. Compare the service names listed in the upstream block with those defined in Docker.
  3. Correct any discrepancies by updating the configuration to have the exact names.
  4. Reload NGINX to apply changes.

Example:

upstream myapp_server {
    server web:80;
}

Note: Make sure ‘web’ is the service name used in Docker to describe the container hosting the application.

Solution 2: Check for DNS Resolution Issues

Verify if the Docker internal DNS is able to resolve the service names by connecting to the NGINX container and using the ping command.

Steps:

  1. Execute docker exec -it nginx_container_name /bin/sh to connect to your NGINX container.
  2. Within the NGINX container, try pinging the service: ping web.
  3. If the ping is unsuccessful, investigate the DNS settings or service definitions in Docker.

This solution only requires verifying the operational state rather than changing configuration files.

Solution 3: Delay NGINX Startup

Configure the start-up sequence of your Docker containers so that NGINX does not try to resolve services before they are available.

Steps:

  1. Define dependencies in your docker-compose.yml using the depends_on option.
  2. Consider using a wait-for-it script for NGINX to delay its start until the required service is up.

Example:

services:
  web:
    image: myapp

  nginx:
    image: nginx
    depends_on:
      - web

In this configuration:

  • You have defined two services: web and nginx.
  • The web service uses an image named myapp.
  • The nginx service uses the nginx image and is set to depend on the web service, meaning nginx will only start after the web service has been started.

Summary

Addressing the ‘Host not found in upstream’ error involves verifying the service names, checking DNS resolution within Docker, and making sure that services have started up in the correct order. By following the provided solutions, you should be able to resolve the error and have a working NGINX reverse proxy setup in your Docker environment.

Next Article: NGINX: How to Execute Shell Commands on Every Request

Previous Article: NGINX error: Conflicting server name – Two or more sites have the same server name

Series: NGINX Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide