Ali Bdeir
Ali Bdeir

Reputation: 4375

502 Bad Gateway when accessing Dockerized ASP.NET Core Web API on Elastic Beanstalk

My docker-compose.override.yml is as follows:

version: '3.4'

services:
  helen.api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:5000
    ports:
      - "5001:443"
      - "5000:80"
    volumes:
      - ~/.aspnet/https:/root/.aspnet/https:ro
      - ~/.microsoft/usersecrets:/root/.microsoft/usersecrets:ro

Dockerfile:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyProjectWebAPI/MyProject.API.csproj", "MyProjectWebAPI/"]
COPY ["MyProject.Globals/MyProject.Globals.csproj", "MyProject.Globals/"]
RUN dotnet restore "MyProjectWebAPI/MyProject.API.csproj"
COPY . .
WORKDIR "/src/MyProjectWebAPI"
RUN dotnet build "MyProject.API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyProject.API.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.API.dll"]

Docker-compose.yml

version: "3.4"

services:
  helen.api:
    image: ${DOCKER_REGISTRY-}helenapi
    build:
      context: .
      dockerfile: HelenWebAPI/Dockerfile

When trying to access any URL of the Dockerized ASP.NET Web API, I get a 502 Bad Gateway Error:

img

Upvotes: 3

Views: 1890

Answers (2)

jccampanero
jccampanero

Reputation: 53461

Perhaps the problem could be motivated that, according to the AWS documentation:

If you manage your Docker environment with Docker Compose, Elastic Beanstalk assumes that you run a proxy server as a container. Therefore it defaults to None for the Proxy server setting, and Elastic Beanstalk does not provide an NGINX configuration.

Note

Even if you select NGINX as a proxy server, this setting is ignored in an environment with Docker Compose. The Proxy server setting still defaults to None.

Please, following the suggested advice, try providing your own nginx container and the corresponding configuration in one of your docker-compose files. For example:

version: '3.4'

services:
  helen.api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:5000
    volumes:
      - ~/.aspnet/https:/root/.aspnet/https:ro
      - ~/.microsoft/usersecrets:/root/.microsoft/usersecrets:ro
  nginx:
    image: nginx:alpine
    volumes:
      - ~/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - helen.api
    ports:
      - "80:80"

Microsoft as well as AWS provide excellent guides about how your nginx.conf file could look like:

server {
    listen        80;
    
    location / {
        proxy_pass         http://helen.api:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

Note we are referencing the container helen.api as the server which is being proxied and that we are using port 5000 as configured in the ASPNETCORE_URLS setting.

Upvotes: 1

DmitriKonnov
DmitriKonnov

Reputation: 41

Elastic Beanstalk's proxy per defaults forwards traffic on port 5000. So either you should configure nginx to forward request to the port, your app listens on - 80, or you should make your app listen on port 5000.

your current config:

ingress traffic ---> 80: nginx :any ---5000 --> ??? 80: your container

should be:

ingress traffic ---> 80: nginx :any ----80 ---> 80: your container

or maybe easier to configure:

ingress traffic ---> 80: ngnix :any ---5000 ---> 5000: your container

The latter can be easier changed by simply changing the environment property of your container.

I'm zero in .NET-terms, but look here:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/dotnet-linux-platform-nginx.html

Upvotes: 2

Related Questions