awslabs / ecs-nginx-reverse-proxy

Reference architecture for deploying Nginx on ECS, both as a basic static resource server, and as a reverse proxy in front of a dynamic application server.
https://containersonaws.com/pattern/nginx-reverse-proxy-sidecar-ecs-fargate-task
Apache License 2.0
329 stars 137 forks source link

nginx: [emerg] host not found in upstream "app" in /etc/nginx/nginx.conf:24 #4

Open cmthomps opened 3 years ago

cmthomps commented 3 years ago

When I attempt to deploy this, the nginx container fails and stops with an error "nginx: [emerg] host not found in upstream "app" in /etc/nginx/nginx.conf:24". It seems like the internal name resolution doesn't work. I was having a similar problem with another project and then just tried to deploy this project as is. Has something changed in ECS since this was developed?

Full log:


timestamp message
1609878836789 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
1609878836789 /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
1609878836792 /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
1609878836795 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
1609878836804 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
1609878836805 /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
1609878836809 /docker-entrypoint.sh: Configuration complete; ready for start up
1609878836823 2021/01/05 20:33:56 [emerg] 1#1: host not found in upstream "app" in /etc/nginx/nginx.conf:24
1609878836823 nginx: [emerg] host not found in upstream "app" in /etc/nginx/nginx.conf:24

dfelski commented 3 years ago

I'm also facing a similar issue trying this approach, it looks like the host name can't be resolved with this setup anymore.

...myapp could not be resolved (3: Host not found)...

htchaan commented 2 years ago

In my case, the hostname lookup failed because the ECS task is not up yet. Maybe you could try adding this to your nginx section:

depends_on:
      - app

Btw from the official docker compose documentation, service.links is not (no longer?) implemented.

oussaka commented 2 years ago

try adding this :

    web:
        image: nginx:alpine
        working_dir: /app
        restart: unless-stopped
        volumes:
            - .:/app
            - ./.docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
        port:
           - ...
        depends_on:
            - php-fpm
        networks:
            - nginx_net

    app:
        image: php:7-fpm
        working_dir: /app
        restart: unless-stopped
        volumes:
            - .:/app
            - ...
        environment:
            ....
        networks:
            - nginx_net
netbrothers-tr commented 1 year ago

Had the same error in a different project. Once I switched from nginx:1.23.3-alpine to nginx:1.23.3 it worked without any further adjustments. Not sure why the alpine image wasn't able to find the host, though.

dzung1nguyen commented 1 year ago

After I changed from "alpine" to "1.22.0-alpine" (didn't check on newer versions), it's working.

xdoctordog commented 1 year ago

Using nginx:1.25.2 but still facing with this issue.

2023/08/23 12:01:33 [emerg] 1#1: host not found in upstream "$server_two" in /etc/nginx/conf.d/alb.conf:32 nginx: [emerg] host not found in upstream "$server_two" in /etc/nginx/conf.d/alb.conf:32

my environment is: Win 10 WSL2 Docker

docker-compose.yml

` version: "3" services: app: image: nginx:1.25.2 container_name: "load_balancer_nginx" ports:

nginx -T

` nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

configuration file /etc/nginx/nginx.conf:

events {}

http { include /etc/nginx/conf.d/*.conf; }

configuration file /etc/nginx/conf.d/alb.conf:

server {
    listen 8000;
    server_name server.one;
    root /var/www/html/server.one/;
    charset utf-8;

    index index.html;
    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 8088;
    server_name server.two;
    root /var/www/html/server.two/;
    charset utf-8;

    index index.html;
    location / {
        try_files $uri $uri/ =404;
    }
}

map $scheme $server_two {
  default server.two:8088;
}

upstream all {
    keepalive 20;
    server server.one:8000 fail_timeout=999s max_fails=999999;
    #server $server_two;
}

server {
    listen 80;

    server_name magento_alb.loc;

    absolute_redirect off;

    location / {
        set $backend_servers all;
        proxy_pass http://$backend_servers;
        #proxy_pass http://$backend_servers:8080;

        #proxy_pass http://all/;

        proxy_pass_request_headers on;
        proxy_set_header Host $host;
        #proxy_set_header Host magento_alb.loc;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;

        proxy_redirect off;
        proxy_intercept_errors on;
        proxy_cache off;
    }
}

configuration file /etc/nginx/conf.d/default.conf:

nothing. just to rewrite the existing one.

`

Could someone help me to figure this out ?

manojniit commented 6 months ago

I'm getting the same error on docker hub, can you help me with this? nginx: [emerg] host not found in upstream "openems-ui" in /etc/nginx/nginx.conf:31

These are my nginx.conf configration file

worker_processes 1;

events { worker_connections 1024; }

http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 5;

    server {
            #listen 80;
            #server_name localhost 127.0.0.1;

            listen 80;
            root /usr/share/nginx/html/;
            rewrite_log on;

            location / {
                    rewrite ^ /m/$args last;
            }

            location /m/ {
                    index index.html;
                    try_files $uri$args $uri$args/ /index.html;
            }

            location / {
            proxy_pass          http://openems-ui:80;
            proxy_set_header    X-Forwarded-For $remote_addr;
        }

        location /backend {
            proxy_pass          http://openems-backend:8079/;
            proxy_set_header    X-Forwarded-For $remote_addr;
        }

        location /edge {
            proxy_pass          http://openems-edge:8080/;
            proxy_set_header    X-Forwarded-For $remote_addr;
        }
        location /ui-edge {
            proxy_pass          http://ui-edge:8082/;
            proxy_set_header    X-Forwarded-For $remote_addr;
        }
        location /ui-backend {
            proxy_pass          http://ui-backend:8082/;
            proxy_set_header    X-Forwarded-For $remote_addr;
        }

    }

}

Docker file

FROM --platform=$BUILDPLATFORM alpine:latest AS ui_builder

RUN apk update && apk upgrade

RUN apk add --no-cache \ npm \ nodejs

RUN npm install -g @angular/cli

FROM --platform=$BUILDPLATFORM ui_builder AS build_ui

ARG UI_VERSION

COPY ./ /src

WORKDIR /src/ui RUN npm install

FROM ghcr.io/linuxserver/baseimage-alpine:edge AS ui_base

RUN apk update && apk upgrade

RUN apk add --no-cache \ nginx \ openssl \ npm \ nodejs

RUN npm install -g @angular/cli

FROM ui_base

RUN mkdir -p /etc/nginx/site-templates /var/log/nginx /var/www/html/openems

COPY --from=build_ui /src/ui/ /opt/ui/src COPY tools/docker/openems-ui/root/ /

COPY tools/docker/openems-ui/nginx.conf /etc/nginx/nginx.conf

COPY --chown=root:root tools/docker/openems-ui/nginx.conf /etc/nginx/nginx.conf

VOLUME /etc/nginx VOLUME /var/log/nginx

ARG HOST ARG WEBSOCKET_PORT

ENV TYPE edge ENV REST_API_PORT 8084

EXPOSE 80 443

yml file

name: Build OpenEMS Docker Image

on: push: paths:

jobs: docker: runs-on: ubuntu-latest strategy: fail-fast: false matrix: image: [openems-edge, openems-backend, openems-ui, ui-backend, ui-edge] include: