<p>I have a .NET 5 API app running a Kestrel server inside a Docker container. I can run the app locally inside of the Docker container without any trouble. The issue occurs when I deploy to Kubernetes which has a LoadBalancer that appears to be causing issues.</p>
<p>I receive the following error(s) repeatedly:</p>
<pre><code>←[40m←[37mdbug←[39m←[22m←[49m: Microsoft.AspNetCore.Server.Kestrel[17]
      Connection id &quot;0HMAMHC4BPDGN&quot; bad request data: &quot;Unrecognized HTTP version: '10.98.6.196 10.123.90.100 62168 443'&quot;
      Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Unrecognized HTTP version: '10.98.6.196 10.123.90.100 62168 443'
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser`1.RejectUnknownVersion(Int32 offset, ReadOnlySpan`1 requestLine)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser`1.ParseRequestLine(TRequestHandler handler, ReadOnlySpan`1 requestLine)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser`1.ParseRequestLine(TRequestHandler handler, SequenceReader`1&amp; reader)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.ParseRequest(SequenceReader`1&amp; reader)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result, Boolean&amp; endConnection)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication`1 application)
←[40m←[37mdbug←[39m←[22m←[49m: Microsoft.AspNetCore.Server.Kestrel[10]
      Connection id &quot;0HMAMHC4BPDGN&quot; disconnecting.
←[40m←[37mdbug←[39m←[22m←[49m: Microsoft.AspNetCore.Server.Kestrel[2]
      Connection id &quot;0HMAMHC4BPDGN&quot; stopped.
←[40m←[37mdbug←[39m←[22m←[49m: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[7]
      Connection id &quot;0HMAMHC4BPDGN&quot; sending FIN because: &quot;The Socket transport's send loop completed gracefully.&quot;
</code></pre>
<p>My kubernetes infrastructure deployment. It is mapping all 443 requests to port 80 of the running Docker container.</p>
<pre><code>kind: ConfigMap
apiVersion: v1
metadata:
  name: api-base
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-base
spec:
  replicas: 1
  selector:
    matchLabels:
      app: api-base
  template:
    metadata:
      labels:
        app: api-base
    spec:
      containers:
      - name: api-base
        image: /path/to/image      
        ports:
        - containerPort: 80
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
   name: api-base
   labels:
      app: api-base
   annotations:
      service.beta.kubernetes.io/aws-load-balancer-internal: 0.0.0.0/0
      service.beta.kubernetes.io/aws-load-balancer-ssl-cert: mycert/path
      service.beta.kubernetes.io/aws-load-balancer-proxy-protocol: &quot;*&quot;
spec:
   selector:
      app: api-base
   ports:
    - port: 443
      targetPort: 80
      protocol: TCP
      name: https
   type: LoadBalancer
</code></pre>
<p>Dockerfile (abbreviated) exposes port 80 and configures .net core to run on port 80.</p>
<pre><code>FROM mcr.microsoft.com/dotnet/aspnet:5.0 
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS http://*:80
COPY --from=publish /app/publish .
ENTRYPOINT [&quot;dotnet&quot;, &quot;API.dll&quot;]
</code></pre>
<p>Startup.cs</p>
<pre><code>public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    private const string CorsOriginName = &quot;AllowedOrigins&quot;;

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddCors(options =&gt;
        {
            options.AddPolicy(name: CorsOriginName,
                builder =&gt;
                {
                    builder.AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader();
                });
        });

        services.Configure&lt;ForwardedHeadersOptions&gt;(options =&gt;
        {
            options.ForwardedHeaders =
                ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
        });

        services.AddControllers();
        services.AddSwaggerGen(c =&gt;
        {
            c.SwaggerDoc(&quot;v1&quot;, new OpenApiInfo { Title = &quot;API&quot;, Version = &quot;v1&quot; });
        });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseSwagger();
        app.UseSwaggerUI(c =&gt; c.SwaggerEndpoint(&quot;/swagger/v1/swagger.json&quot;, &quot;API v1&quot;));

        app.UseCors(CorsOriginName);
        app.UseForwardedHeaders();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =&gt;
        {
            endpoints.MapControllers();
        });
    }
}
</code></pre>
<p>Program.cs</p>
<pre><code>public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
        Host.CreateDefaultBuilder(args)
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
            .ConfigureAppConfiguration((hostContext, builder) =&gt;
            {
                // Add other providers for JSON, etc.
                if (hostContext.HostingEnvironment.IsDevelopment())
                {
                    builder.AddUserSecrets&lt;Program&gt;();
                }
            })
            .ConfigureWebHostDefaults(webBuilder =&gt;
            {
                webBuilder.UseKestrel().UseStartup&lt;Startup&gt;();
            });
 }
</code></pre>
<p>How do I configure Kestrel to accept requests from the LoadBalancer?</p>
<p><strong>Update:</strong></p>
<p>To accomplish this I terminate https traffic and communicate with the container using http</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: api-base
  annotations:
    # Note that the backend talks over HTTP.
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: http
    # TODO: Fill in with the ARN of your certificate.
    service.beta.kubernetes.io/aws-load-balancer-ssl-cert: arn:aws:acm:{region}:{user id}:certificate/{id}
    # Only run SSL on the port named &quot;https&quot; below.
    service.beta.kubernetes.io/aws-load-balancer-ssl-ports: &quot;https&quot;
spec:
  selector:
    app: api-base
  ports:
  - name: http
    port: 80
    targetPort: 80
  - name: https
    port: 443
    targetPort: 80
  type: LoadBalancer
</code></pre>
<p><a href="https://aws.amazon.com/premiumsupport/knowledge-center/terminate-https-traffic-eks-acm/" rel="nofollow noreferrer">https://aws.amazon.com/premiumsupport/knowledge-center/terminate-https-traffic-eks-acm/</a></p>

<p>You need to decide if you want to terminate https on your application or before that. I would suggest that you use Ingress instead of LB type service to expose your service to external world. That way you can configure https termination on the ingress it self and the app would remain http based with no need to configure certs, encryption etc. on the app it self, although you would still need to configure ingress properly.</p>
