【问题标题】:.NET Core 3 Docker Project Generates Too Many 307 Redirects Before Failing.NET Core 3 Docker 项目在失败前生成太多 307 重定向
【发布时间】:2019-11-29 20:47:52
【问题描述】:

启动部署到 Google Application Engine Flexible 的应用程序时,由于 307 重定向过多而失败。它在 VS IDE 本地成功运行。

开发和计算堆栈包括:

  • MacOS
  • .NET Core 3
  • Visual Studio 2019 for Mac
  • 码头工人
  • 谷歌应用引擎

我使用 VS api 模板(天气预报)创建了一个项目。

  • 创建 API 项目
  • 添加 Docker 支持(通过菜单)
  • 创建和导出 SSL 证书:
dotnet dev-certs https -v -ep /Users/QQQQQQQ/Projects/CostZzzzzzzzzz/xxxxx.Orchestration.Cost/Certificate/dev-certificate.pfx -p ufo

(我随后将其移至项目的根目录)

  • 修改Dockerfile如下:
    FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    EXPOSE 8080

    FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
    WORKDIR /src
    COPY Xxxxx.Orchestration.Cost/Xxxxx.Orchestration.Cost.csproj Xxxxx.Orchestration.Cost/
    RUN dotnet restore "Xxxxx.Orchestration.Cost/Xxxxx.Orchestration.Cost.csproj"
    COPY . .
    WORKDIR "/src/Xxxxx.Orchestration.Cost"
    RUN dotnet build "Xxxxx.Orchestration.Cost.csproj" -c Release -o /app/build

    FROM build AS publish
    RUN dotnet publish "Xxxxx.Orchestration.Cost.csproj" -c Release -o /app/publish

    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENV ASPNETCORE_ENVIRONMENT=Development
    ENV ASPNETCORE_URLS=http://*:8080;https://*:443
    ENV ASPNETCORE_HTTPS_PORT=443
    ENV ASPNETCORE_Kestrel__Certificates__Default__Path=Xxxxx.Orchestration.Cost/dev-certificate.pfx
    ENV ASPNETCORE_Kestrel__Certificates__Default__Password=ufo
    ENTRYPOINT ["dotnet", "Xxxxx.Orchestration.Cost.dll"]
  • 创建 app.yaml

    runtime: custom
    env: flex

    # This sample incurs costs to run on the App Engine flexible environment.
    # The settings below are to reduce costs during testing and are not appropriate
    # for production use. For more information, see:
    # https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
    manual_scaling:
      instances: 1
    resources:
      cpu: 1
      memory_gb: 0.5
      disk_size_gb: 10

    network:
        name: default
        subnetwork_name: default-us-east1

    service: get-cost

    env_variables:
      # The __ in My__Greeting will be translated to a : by ASP.NET.
      My__Greeting: Hello AppEngine Flex!

  • 修改 Program.cs 以支持 kestrel 和 ssl
     public class Program
    {
        public static void Main(string[] args)
        {
            //CreateHostBuilder(args).Build().Run();
            CreateWebHostBuilder(args).Build().Run();

        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseKestrel(options =>
             {
                 //     //options.Listen(IPAddress.Loopback, 5000);  // http:localhost:5000
                 options.Listen(IPAddress.Any, 8080);         // http:*:80
                 options.Listen(IPAddress.Any, 443, listenOptions =>
                    {
                        listenOptions.UseHttps("dev-certificate.pfx", "ufo");
                    });

             })
            .UseStartup<Startup>();
    }
  • 将服务部署到 GAE:gcloud app deploy

此解决方案是几篇描述如何通过 Docker 创建 .NET Core 应用程序并将其部署到 GAE 的文章的合并。

错误日志的主要信息是: XXX.YYY.ZZZ.AAA - "GET /" 307 undefined "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"

我正在寻求有关如何正确配置应用程序以使其在 GAE 中正确运行的帮助。

PS:通过删除 Dockerfile 中的 ENV 指令,docker 容器将在我的 mac 上本地运行。但是,在 GAE 上运行它让我望而却步。

【问题讨论】:

    标签: docker google-app-engine .net-core


    【解决方案1】:

    事实证明,一旦我了解真正的问题是什么,解决方案就非常简单,其中一部分源于对较新的 .net 核心版本(如 3.x)很新,对 GAE Flex 是新的,以及Docker,同时进行。

    无论如何,删除 app.UseHttpsRedirection();在 Startup.cs 类中的 Configure 方法解决了眼前的问题。这个问题在这篇文章中有解释:https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-3.1&tabs=visual-studio

    本质上,GAE Flex 已经在端口 8080 上提供重定向,因此代码中的附加重定向指令会导致无休止的重定向,并导致 HTTP 307 结果。

    【讨论】:

    • 嗨@tonyatl,找到解决方案做得很好!你能accept your own answer吗?它将使其更加明显,并在您找到解决方案时帮助遇到相同问题的人。谢谢!
    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 2018-10-10
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多